PowerShell Arrays

You probably use it all the time in your scripts, but how do they work? In this blog post, I will show some simple examples of what you can do with them 🙂

What are PowerShell Arrays?

“An array is a data structure that’s designed to store a collection of items. The items can be the same type or different types.

Beginning in Windows PowerShell 3.0, a collection of zero or one object has some properties of arrays”

Source: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_arrays?view=powershell-7.4

How do they work?

Basic usage for showing data

If you have a variable in which you store data, it will change to that type. In the example below, I store the results from Get-ChildItem in the variable $array. After storing, I use .Gettype() to show the data type of the variable $array.

As you can see, the Object’s base type is System.Array. When using $array, it will show the output of Get-ChildItem of my Array folder, which contains three TXT files.

To show the number of items in the variable $array, you can use the Count property. For example, using $variable.count will return 3:

If you want only to show the first item of the $array variable, you can use $array[0]. It starts counting at 0, and you can also use $array[1] or $array[2] to display the other items.

You can also show a range of items; in the example below, I only want to show items 1 and 2. You specify the range using double dots like this: $array[1..2]. For example:

Alternatively, you can use the minus sign to retrieve the last items of the $array variable. For example, using $array[-1..-2] will show this:

Looping through an array

With foreach, you can use and act on all the items in the array $array. For example:

Adding items to an Array

You can add more objects to an existing array with the same properties. In the example below, I created a $array2 variable with the contents of the Array2 folder (Also three files):

As you can see, setting $array to be $array plus the newly created $array2 shows that it has 6 items using the Count method and will show File1.txt – File6.txt.

Removing items of an Array

You can remove a specific item from an Array by setting it to $null. For example, below I remove the File6.txt from the Array by using $array[5]=$null:

Clearing the contents of an Array

You can completely remove all the items of an Array by using the Clear method and resetting the $array variable when using $array.Clear():

In the example above, I use $array.Clear() to remove everything, and the type of the variable $array is still System. Array but without contents.

Show all Methods and Properties of an Array

To show all the things you can do or retrieve from the $array variable, you can use Get-Member, as shown in the example below:

As you can see in the picture above, there are many methods and properties that you can use from the $array variable. Using Methods is something I will write about in the future, but accessing properties is simple. For example, using $array.Directory will show all the Directory properties from all items inside the array $array:

But to get that information from a specific item, you can use $array[0].Directory, for example:

Wrapping up

In this blog post, I showed you how an Array works and a few things you can do. For more in-depth details, check out the Microsoft Learn document about it 🙂 Enjoy your weekend!

2 thoughts on “PowerShell Arrays

  1. Very nice Harm. I went through this article this morning while drinking some coffee. Had my terminal side by side going through all these exercises. As always, best PS blogger!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.