Difference between revisions of "Finding the length of an array"

From MyWiki
Jump to: navigation, search
(Created page with "Using the ".length"")
 
Line 1: Line 1:
Using the ".length"
+
Using the ".length" will only correctly give the number of items in an array of there is more that one item.<br>
 +
If the item has one item this method will fail.<br>
 +
 
 +
<source lang="powershell">
 +
$a = ls *.bak|foreach {$_.name}
 +
# Replace above with below
 +
$a = @($a = ls *.bak|foreach {$_.name})
 +
</source>

Revision as of 10:22, 1 April 2016

Using the ".length" will only correctly give the number of items in an array of there is more that one item.
If the item has one item this method will fail.

$a = ls *.bak|foreach {$_.name}
# Replace above with below
$a = @($a = ls *.bak|foreach {$_.name})