Skip to the main content.

Unlocking the Power of PowerShell: Tips for Success

Understanding PowerShell Arrays

PowerShell arrays are a powerful and versatile way of managing collections of data, enabling you to efficiently automate tasks and handle complex data structures with ease. Learn more about the fundamentals of PowerShell arrays!

Whenever you store multiple objects in a variable, PowerShell automatically creates an array for you. This array is of the most basic type, capable of storing any type of data, including mixed types: [Object[]].


PS> $test = 1, 100, 'Hello', (Get-Date)
PS> $test.GetType().FullName System.Object[]
PS> $test.Count 4
PS> $test -is [Array] True

 

All arrays use a numerical index starting at 0 to access their elements. What’s special about PowerShell is that it allows you to use multiple indices and negative indices to count from the end of the array.


PS> $test = 1,4,9,100,200
PS> $test[0,-1] 1 200
PS> $test[0,-1] | Measure-Object -Sum | Select-Object -ExpandProperty Sum 201

 

Negative indices provide a convenient way to avoid common workarounds found in other scripting languages. Here are two examples: using the -split operator to create an array and then extracting the last element (the file name in this case).


# start with some string that is a path
PS> $profile.AllUsersCurrentHost C:\Program Files\PowerShell\7\Microsoft.PowerShell_profile.ps1
# split the path to get an array PS> $profile.AllUsersCurrentHost -split '\\' # escape "\" with another "\" because
the argument is interpreted as RegEx C: Program Files PowerShell 7 Microsoft.PowerShell_profile.ps1
# store result in variable for later use PS> $result = $profile.AllUsersCurrentHost -split '\\'
# old and cumbersome generic approach to get the last element PS> $result[$result.Count - 1] Microsoft.PowerShell_profile.ps1
# elegant PowerShell approach to get the last element PS> $result[-1] Microsoft.PowerShell_profile.ps1

Since PowerShell operators are designed to work seamlessly with arrays, they together form a powerful problem-solving tool. For example, all comparison operators act as filters when applied to an array:


# filtering with comparison operators PS> 1,2,3,4,8,9,10,3,2,1 -eq 2 2 2
PS> (1,2,3,4,8,9,10,3,2,1 -eq 2).Count 2
PS> 1,2,3,4,8,9,10,3,2,1 -contains 2 True
PS> (ipconfig) -like '*IPv4*' IPv4 Address. . . . . . . . . . . : 10.5.0.2 IPv4 Address. . . . . . . . . . . : 192.168.3.14 IPv4 Address. . . . . . . . . . . : 172.30.96.1
# creating arrays with -split, then filtering with indices and comparison PS> 'CN=Tobias,OU=Trainer,DC=powershell,DC=one,DC=local' -split ',' CN=Tobias OU=Trainer DC=powershell DC=one DC=local
PS> ('CN=Tobias,OU=Trainer,DC=powershell,DC=one,DC=local' -split ',')[0] CN=Tobias
PS> ('CN=Tobias,OU=Trainer,DC=powershell,DC=one,DC=local' -split ',')[0] -split '='
| Select-Object -Last 1 Tobias
# extracting all domain components from X500 PS> 'CN=Tobias,OU=Trainer,DC=powershell,DC=one,DC=local' -split ',' -like 'DC=*' DC=powershell DC=one DC=local
PS> 'CN=Tobias,OU=Trainer,DC=powershell,DC=one,DC=local' -split ',' -like 'DC=*' -join ',' DC=powershell,DC=one,DC=local
# extracting X500 domain name as netbios name PS> 'CN=Tobias,OU=Trainer,DC=powershell,DC=one,DC=local' -split ',' -like 'DC=*' -replace 'DC=' -join '.' powershell.one.local

 

Where there is light, there is also darkness. These are the two common limitations of the default arrays used in PowerShell:

  • You can’t always predict the result: PowerShell only auto-creates an array if there is more than one result. Otherwise, it may return a single object.
  • Default arrays in PowerShell are read-only. You cannot delete elements or add new ones without causing significant performance issues or resorting to unconventional methods.

 

These two limitations will be discussed in part 2 of this series.

 

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

4 min read

Bulk Testing PowerShell Scripts with the Tokenizer

In Part 1, we explored the internal PowerShell parser to see how it analyzes code and breaks it down into individual...

3 min read

Mastering PowerShell Tokenization for Efficient Scripting

The internal PowerShell parser processes any code before execution. With full access to this parser, you can analyze...

3 min read

Using .NET Libraries in PowerShell - Functions, Cmdlets and .NET

In part 3, we identified a useful .NET method to display system dialogs and then wrapped it inside a new PowerShell...

About the author: