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...
Unlocking the Power of PowerShell: Tips for Success
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:
These two limitations will be discussed in part 2 of this series.
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.
Apr 16, 2025 by Aleksandar Nikolić and Dr. Tobias Weltner
In Part 1, we explored the internal PowerShell parser to see how it analyzes code and breaks it down into individual...
Apr 11, 2025 by Aleksandar Nikolić and Dr. Tobias Weltner
The internal PowerShell parser processes any code before execution. With full access to this parser, you can analyze...
Apr 3, 2025 by Aleksandar Nikolić and Dr. Tobias Weltner
In part 3, we identified a useful .NET method to display system dialogs and then wrapped it inside a new PowerShell...
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.