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...
Unlocking the Power of PowerShell: Tips for Success
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
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.
Feb 10, 2025 by Aleksandar Nikolić and Dr. Tobias Weltner
Optimizing PowerShell arrays is key to improving script performance and ensuring efficient data handling in your...
Jan 28, 2025 by Aleksandar Nikolić and Dr. Tobias Weltner
Understanding the different array types in PowerShell is essential for efficient data management and automation in your...
Jan 16, 2025 by Aleksandar Nikolić and Dr. Tobias Weltner
Here is part two of our tips on PowerShell arrays.
Let’s explore some common caveats with arrays, starting with the...
Tobias Weltner and Aleksandar Nikolić joinly wrote the blog post series 'Tobias&Aleksandar's PowerShell tips'. So we introduce both of them here:
----------------------------
Aleksandar Nikolić is a Microsoft Azure MVP and co-founder of PowerShellMagazine.com, the ultimate online source for PowerShell enthusiasts. With over 18 years of experience in system administration, he is a respected trainer and speaker who travels the globe to share his knowledge and skills on Azure, Entra, and PowerShell. He has spoken at IT events such as Microsoft Ignite, ESPC, NIC, CloudBrew, NTK, and PowerShell Conference Europe.
----------------------------
Tobias is a long-time Microsoft MVP and has been involved with the development of PowerShell since its early days. He invented the PowerShell IDE "ISESteroids", has written numerous books on PowerShell for Microsoft Press and O'Reilly, founded the PowerShell Conference EU (psconf.eu), and is currently contributing to the advancement of PowerShell as member in the "Microsoft Cmdlet Working Group". Tobias shares his expertise as a consultant in projects and as a trainer in in-house trainings for numerous companies and agencies across Europe.