Skip to the main content.

Unlocking the Power of PowerShell: Tips for Success

Optimizing PowerShell Performance: Using More Efficient and Flexible Arrays

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,4 PS> $result = Get-Service PS> @($list, $result) | Get-Member | Select-Object -Property TypeName -Unique TypeName -------- 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 -Unique TypeName -------- System.String[]

 

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


PS> $list.IsFixedSize True PS> $parts.IsFixedSize True PS> $result.IsFixedSize True

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,8 PS> $list = $list -ne 2 -ne 5 -ne 8 PS> $list 1 3 4 6 7 PS> # add new array elements PS> $list += 100 PS> $list 1 3 4 6 7 100

 

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.IsFixedSize False

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. You’ve 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 1 added 6 added 10001 added 100000

Good2know

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.

PowerShell Poster 2023

Get your poster here!

 

 

Related links 

 

Related posts

3 min read

Optimizing PowerShell Performance: Using More Efficient and Flexible Arrays

Optimizing PowerShell arrays is key to improving script performance and ensuring efficient data handling in your...

3 min read

Typed PowerShell Arrays and Generic Lists

Understanding the different array types in PowerShell is essential for efficient data management and automation in your...

3 min read

PowerShell Array Caveats: Common Issues and Solutions

Here is part two of our tips on PowerShell arrays.

Let’s explore some common caveats with arrays, starting with the...

About the author: