Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /homepages/27/d281301701/htdocs/InsideMicrosoft/blog/wp-content/plugins/wordfence/models/block/wfBlock.php on line 536

Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /homepages/27/d281301701/htdocs/InsideMicrosoft/blog/wp-content/plugins/wordfence/models/block/wfBlock.php on line 537

Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /homepages/27/d281301701/htdocs/InsideMicrosoft/blog/wp-content/plugins/wordfence/models/block/wfBlock.php on line 539

Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /homepages/27/d281301701/htdocs/InsideMicrosoft/blog/wp-content/plugins/wordfence/models/block/wfBlock.php on line 554

Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /homepages/27/d281301701/htdocs/InsideMicrosoft/blog/wp-content/plugins/wordfence/models/block/wfBlock.php on line 557

Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /homepages/27/d281301701/htdocs/InsideMicrosoft/blog/wp-content/plugins/jetpack/_inc/lib/class.media-summary.php on line 77

Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /homepages/27/d281301701/htdocs/InsideMicrosoft/blog/wp-content/plugins/jetpack/_inc/lib/class.media-summary.php on line 87

Warning: count(): Parameter must be an array or an object that implements Countable in /homepages/27/d281301701/htdocs/InsideMicrosoft/blog/wp-includes/kses.php on line 893

Warning: count(): Parameter must be an array or an object that implements Countable in /homepages/27/d281301701/htdocs/InsideMicrosoft/blog/wp-includes/kses.php on line 893

Warning: count(): Parameter must be an array or an object that implements Countable in /homepages/27/d281301701/htdocs/InsideMicrosoft/blog/wp-includes/kses.php on line 893

Warning: count(): Parameter must be an array or an object that implements Countable in /homepages/27/d281301701/htdocs/InsideMicrosoft/blog/wp-includes/kses.php on line 893

Warning: count(): Parameter must be an array or an object that implements Countable in /homepages/27/d281301701/htdocs/InsideMicrosoft/blog/wp-includes/kses.php on line 893

Warning: count(): Parameter must be an array or an object that implements Countable in /homepages/27/d281301701/htdocs/InsideMicrosoft/blog/wp-includes/kses.php on line 893

Warning: count(): Parameter must be an array or an object that implements Countable in /homepages/27/d281301701/htdocs/InsideMicrosoft/blog/wp-includes/kses.php on line 893

Warning: count(): Parameter must be an array or an object that implements Countable in /homepages/27/d281301701/htdocs/InsideMicrosoft/blog/wp-includes/kses.php on line 893

Warning: count(): Parameter must be an array or an object that implements Countable in /homepages/27/d281301701/htdocs/InsideMicrosoft/blog/wp-includes/kses.php on line 893

Warning: count(): Parameter must be an array or an object that implements Countable in /homepages/27/d281301701/htdocs/InsideMicrosoft/blog/wp-includes/kses.php on line 893

Warning: count(): Parameter must be an array or an object that implements Countable in /homepages/27/d281301701/htdocs/InsideMicrosoft/blog/wp-includes/kses.php on line 893

Warning: count(): Parameter must be an array or an object that implements Countable in /homepages/27/d281301701/htdocs/InsideMicrosoft/blog/wp-includes/kses.php on line 893
Best Practices for Getting the Count of Items in a PowerShell Collection - Inside Microsoft

Best Practices for Getting the Count of Items in a PowerShell Collection


Warning: Use of undefined constant php - assumed 'php' (this will throw an Error in a future version of PHP) in /homepages/27/d281301701/htdocs/InsideMicrosoft/blog/wp-content/themes/insidemicrosoft/single.php on line 36

One powerful aspect of PowerShell is that it is a loosely typed language. The advantage to script developers is that they can assign an untyped variable to almost anything. This makes script development faster and usually easier because variable can be assigned to almost anything without having to fully understand the underlying data types.

The Problem

This advantage however is another example of PowerShell giving authors just enough rope to hang themselves! Scripts will seemingly work until an untyped variable is assigned a value that is a type that the script author did not intend (or test).

I recently got caught in this trap when getting the number of items returned from a cmdlet.  For this example we’ll get all the processes running and group by the process name which will give us the number of unique process (names) running:

$myProcessArray = Get-Process | Group-Object ProcessName

$myProcessArray.Count

73

In other words, there are 73 processes running (with a unique Process Name).

The trouble comes in cases where the $myProcessArray is manipulated so that only ONE ITEM IS RETURNED and assigned to a new array. In this case, PowerShell will NOT return 1 (item in the new array).  Because there is only 1 item returned, PowerShell will usually consider this an object (in the case a System.Diagnostics.Process object) and return the number of properties instead of array items.

The Solution

The best way to avoid problems such as this is to adopt the best practice of strongly typing the output variable to the type we are expecting – in this case, an array.  We do this by wrapping enclosing the script that generates the data with the “@” symbol which forces it into an array like this:

$myProcessArray = @{Get-Process | Group-Object ProcessName}

By forcing the output variable to be an array, it will always be treated like one and the Count property will accurately reflect the number of elements.  It will also never be $null.

PowerShell Picture

category:

Leave a Reply