Optimizing PowerShell arrays is key to improving script performance and ensuring efficient data handling in your automation tasks. Let's dive into it.

By default, PowerShell uses the most basic type of array: [Object[]]. This is beneficial because it can store any type of data.

PS> $list = 1,2,3,4PS> $result = Get-ServicePS> @($list, $result) | Get-Member | Select-Object -Property TypeName -UniqueTypeName -------- System.Object[]

Occasionally, specific .NET methods, such as Split(), return more specialized arrays, like [string[]]. These behave similarly to [object[]], except that they can only store strings:

PS> @(,$parts) | Get-Member | Select-Object -Property TypeName -UniqueTypeName -------- System.String[]

All of these arrays have one thing in common: they cannot shrink or grow.

PS> $list.IsFixedSizeTruePS> $parts.IsFixedSizeTruePS> $result.IsFixedSizeTrue

However, you can still remove elements from such arrays or add new ones:

PS> #remove unwanted array elements PS> $list = 1,2,3,4,5,6,7,8PS> $list = $list -ne 2 -ne 5 -ne 8PS> $list13467 PS> # add new array elementsPS> $list += 100PS> $list 13467100

While this works, it's essentially a workaround: you create new arrays of the appropriate size and copy the old content over. This approach works well for single operations but can cause significant slowdowns and resource hogging when performed in loops:

# start with empty array$array = @()# do 100000 iterations for illustration$list = 1..100000# add elements to array using "+="foreach ($element in $list){ $array += "added $element"}# should be 100.000 array elements now$array.Count

This simple script effectively hangs Windows PowerShell. Even in the latest PowerShell 7.5, while the issue has been improved, the code still takes far too long to execute.One way to address this is by using more efficient array types. A commonly used type is [System.Collections.ArrayList], which is no longer of fixed size:

PS> [System.Collections.ArrayList]$al = @()PS> $al.IsFixedSizeFalse

With this type of array, which allows you to add and remove elements, you no longer need to resort to the "dirty tricks" used by the += operator:

# start with empty array[System.Collections.ArrayList]$array = @()# do 100000 iterations for illustration$list = 1..100000# add elements to array using "+="foreach ($element in $list){ # NO: $array += "added $element" # YES: array can now add new elements directly # it returns the index where the new element was inserted # which you can assign to $null if you don't need it $null = $array.Add("added $element")}# should be 100000 array elements now$array.Count

Now, the script executes almost instantly. Youve successfully avoided the dreaded += slowdown and used a more powerful array type that can add and remove elements on the fly.

The other powerful PowerShell array tricks still work with your new super array type:

PS> $array[0,5,10000,-1]added 1added 6added 10001added 100000

Your ultimate PowerShell Cheat Sheet

Unleash the full potential of PowerShell with our handy poster. Whether you're a beginner or a seasoned pro, this cheat sheet is designed to be your go-to resource for the most important and commonly used cmdlets.The poster is available for download and in paper form.

Get your poster here!

Related links