PowerShell arrays are an essential feature of the PowerShell scripting language. But if you haven’t used one (or haven’t realized that you have), you might need to get a little education on the topic!

What is an array exactly? Arrays are collections of related items, such as a list of numbers or names, that are stored in a variable. By using arrays, you can store and manipulate large amounts of data in your scripts. In this article, we will cover the basics of creating, indexing, and iterating over arrays in PowerShell.

As a simple example, you could get a list of all of the services running on a workstation, or all of the files in a directory. Those objects can be stored in a single variable, and you can work with the whole collection like it’s one big object!

Creating Arrays

To create an array in PowerShell, you need to:

  1. declare the array variable (optional)
  2. assign it a value (super-easy!)

From my examples above with the services or files, let’s use a quick demo:

Here I’m creating a service variable in two different ways. The first way saves a single service into a variable called $Service. The second way gets all the services and saves them into a variable called $Services. There were a couple of services that flashed red because the PowerShell session wasn’t running as admin. No worries.

Notice that when I run the built in method “GetType()” on both variables, the type of object stored in the $Service variable is listed as a Service Controller. But in the variable with multiple services in it, it was listed as an Array.

Demonstration of creating an array in PowerShell

So this is probably the most common way that you’ll create an array – you’ll just grab some objects and store them in a variable. But every so often you’ll want to create an empty array, and then add objects to it later.

There are a few ways to create an empty array in PowerShell, such as using the @() notation, the New-Object cmdlet, or by casting a range of values as an array. It is important to follow best practices for naming your array variables, such as using descriptive names and avoiding using reserved keywords.

If you do need to create an empty array, I recommend this simple, straightforward approach:

$ArrayVariable = @()

Indexing Arrays

Indexing refers to accessing the individual items in an array. In PowerShell, the index numbers start at 0, meaning that the first item in the array has an index of 0, the second item has an index of 1, and so on. You can also use negative index numbers to access items from the end of the array.

Demonstration of indexing a PowerShell array.

Iterating Over Arrays

Iterating over an array means processing each item in the array. PowerShell provides two main ways to iterate over an array: ForEach-Object and For loops. ForEach-Object is a cmdlet that iterates over a collection of objects and performs an operation on each item. For loops are a more traditional loop structure that can be used to iterate over an array.

# Create an array
$Array = [0..10]

# Using a For loop
# Uses a second variable ($i is typical) as a counter. Then the counter is used as a reference to index the array.
For ($i = 0; $i -le $Array.length; $i++) { $Array[$i] } 

# Using the Foreach-Object
# Foreach (an alias for Foreach-Object) is used to process each item of an array. In this example the $_ (means "the current item in the pipeline")
# and it just outputs that object
$Array | Foreach {$_}

Practical Applications of Arrays in PowerShell

Arrays are a powerful tool in PowerShell scripts. They can be used for a variety of tasks, such as reading and writing data to files, processing large amounts of data, and performing complex calculations. By using arrays, you can simplify your code and make it more efficient.

Conclusion

In conclusion, PowerShell arrays are a fundamental concept that every PowerShell scripter should master. With arrays, you can store and manipulate collections of data, iterate through them, and even perform complex operations on them. You can also use arrays to optimize your scripts and make them more efficient.

By learning PowerShell arrays, you are taking the first step towards mastering PowerShell and becoming a more efficient and productive IT professional. So, keep learning, keep practicing, and don’t be afraid to ask for help. There are many excellent resources available to help you improve your PowerShell skills, including online courses, tutorials, and communities.

Whether you are just starting out with PowerShell or have been using it for years, there is always something new to learn. So, keep exploring, keep experimenting, and most importantly, keep having fun! With enough practice and dedication, you can become a PowerShell expert and take your IT career to the next level.