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...
Unlocking the Power of PowerShell: Tips for Success
Understanding the different array types in PowerShell is essential for efficient data management and automation in your scripting endeavors.
So far, all array types haven’t been specific, allowing you to store "anything." For added efficiency and data integrity, you can also use typed arrays.
To create a typed array, simply specify the desired type and add “[]”. This creates an array that can only store values of that type. Note how floating-point values are automatically converted to whole numbers:
PS> [int[]]$id = 1,2,3,4,5.1,6.3,7
PS> $id
1
2
3
4
5
6
7
This example uses a [string[]]: a user can enter ten names, and if a number is accidentally added, PowerShell will automatically convert it to a string:
# new empty string array
[string[]]$names = @()
# let user enter 10 names
for ($x=1; $x-le10;$x++)
{
$names += Read-Host -Prompt "Enter $x. Name"
}
$names.Count
What’s most important about the last example is the reappearance of the dreaded “+=” problem we previously discussed: how can you use a typed array that can dynamically grow or shrink? [System.Collections.ArrayList] is not typed.
The solution lies in generic lists, which are very simple to use. Simply take [System.Collections.Generic.List[XXX]] and replace XXX with the type you want to use. This gives you a typed array that can grow or shrink:
PS> $list = [System.Collections.Generic.List[string]]::new()
PS> $list.IsFixedSize
False
The example above can now be rewritten to avoid using “+=”. Keep in mind that this example is meant to illustrate the issue; with just 10 iterations, as shown here, you could likely get away with using “+=”. However, remember the examples from the previous section where “+=” would hang Windows PowerShell.
Here is the rewritten example using a generic list:
# new empty string array
[System.Collections.Generic.List[string]]$names = [System.Collections.Generic.List[string]]::new()
# let user enter 10 names
for ($x=1; $x-le10;$x++)
{
$userinput = Read-Host -Prompt "Enter $x. Name"
$names.Add($userinput)
}
$names.Count
Notice that generic lists do not return anything when you add new data. Unlike [System.Collections.ArrayList], there is no need to assign the result to $null this time.
Generic lists also support all the useful PowerShell array tricks discussed in the first part of this series:
PS> $names[0,1,-2,-1]
Henny
Frank
Alex
Tobi
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
Understanding the different array types in PowerShell is essential for efficient data management and automation in your...
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 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.