Skip to the main content.

Unlocking the Power of PowerShell: Tips for Success

Typed PowerShell Arrays and Generic Lists

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

 

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

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

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

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: