What is a PowerShell Array?
A PowerShell array is a data structure that stores a collection of items. The items stored may be the same or different types.
Creating a PowerShell array is as simple as creating a variable and adding items (in double quotes “”) into the variable separated by commas (,).
As an example, to create a PS array called $ArrayEg, run the command bellow:
$ArrayEg = "A","B","C","D"
Note that the items on the right side of the equation is in double quotes, separated by commas. To confirm that this created a PS Array, run the command below:
The result is shown below:
Notice that the BaseType is System.Array.
If you are creating a PowerShell script Arrays are very helpful. Let’s say you need to add some Active Directory users to a set of security groups. You can create an array containing the AD groups as shown below:
$Groups = "Group1", "Group2", "Group3"
With the security groups in a Array, you can iterate through the array items using a ForEach loop. A ForEach loop will allow you to add the users into each security group in the Array.
Manipulating an Array
A PS Array has some characteristics that you may want to know about. For instance, you might want to find the data type of a PowerShell array. To find the data type of a PowerShell array, use the GetType() method. For example, to get the data array type of ‘$Groups’ PowerShell array created earlier, run the command below:
You can also find additional properties contained in a PowerShell array by piping the array into the Get-Member cmdlet. You might be wondering why you need all these information about an array. Later in the tutorial I will provide additional examples that will clarify why you may need additional information about the Array you are working with.
To find all properties and methods in the PowerShell array $Groups, simply pipe $Groups to the Get-Member cmdlet. See the command below:
$Groups | Get-Member
Result of the previous command is shown below:
In the image notice that there are properties and methods (some part of the image is not shown). Properties represents the actual properties of the PowerShell array. Methods allow you to run commands against the array as shown with the GetType() command previously used in this article.
To learn more about object properties, methods and other PowerShell scripting skills, refer to the references at the end of this page.
How to Access the Items in a PowerShell Array
Earlier in this tutorial, I defined a PS Array as a data structure that stores a collection of items. We also created a simple PowerShell array called $ArrayEg. Let’s revisit this one more time. To proceed, execute the command below:
$ArrayEg = "A", "B", "C", "D"
To list the content of the array, simply execute $ArrayEg as shown below:
The previous command will list the values A, B, C, D in that order. There might be circumstances when you need to access one of the items contained in the array. Before I show you how to run the command that returns the items contained in the PowerShell array, it is important to understand how the array contents are arranged.
You can access the elements in an array by using an index. The first value (position zero) of the PowerShell array is assigned a zero ‘0’. So, item ‘A’ occupies position zero, ‘B’, position one ‘1’ and so on. To return the first item in the Array, run the command below:
The command returns ‘A’ as shown below:
To return the second item, ‘B’, replace ‘0’ with ‘1’ in the previous command.
Practical Application of a PowerShell Array
Up to this point, I have been showing you the theoretical side of a PS Array. But as a Windows Admin I believe you will be keen to learn how you can apply this to a real task.
To this end, I will now show you one application of PowerShell Arrays in reporting processes that are consuming the most resources in a computer.
PowerShell Array Application in Reporting Top CPU Processes
In this example, I will demonstrate how you can use the knowledge you have gained so far about PowerShell Array to find the process that has the top CPU utilization in your pc.
The first step is to get all the process and store them in a variable called $Proc. This is achieved by the command below:
$Proc = Get-Process
The command simply stores the processes on my local pc in the $Proc variable. To see the information stored, execute the command below.
The result of the previous command is shush below:
Take a closer look at CPU(s) column. This column shows the CPU consumption of each process.
To confirm that the previous command created an Array, execute the command below:
How to Find the Processes with the Highest CPU Consumption
Before we can find the processes with top CPU consumption, we need to sort the process in order of CPU consumption. To achieve this, pipe the $Proc PowerShell array to the Sort-Object cmdlet. The Sort-Object cmdlet sorts the content of the PowerShell array in ascending order. Below is the command.
$Proc | Sort-Object CPU
Unfortunately the previous command did not sort the items in the order we want them. See the result of the command below.
We will like to have the items sorted in descending order – so that processes with top CPU utilization are on top of the list.
Sorting the items in descending order is very simple. It requires us to include the Descending parameter of the Sort-Object cmdlet. See the amended command below.
$Proc | Sort-Object CPU -Descending
Here is the result of the command
Notice that the items are now sorted in order of CPU consumption. The process with the highest CPU utilization is now on top. At this point, we can return the process with top CPU utilization. The command below does the magic!
($Proc | Sort-Object CPU -Descending)
This was achieved by returning the first item in the array – that is after it has been sorted. See image below for clarification.
The will return the first item in the PowerShell array. This is the process with top CPU consumption. How good is that!
PowerShell Add to Array
In the final part of this tutorial I will like to show you how to add items to a PowerShell Array. Adding an item to an existing PS Array requires the use of the Add() method.
To illustrate this lets get back to our $ArrayEg example. To create the PS Array $ArrayEg, we executed the command below:
$ArrayEg = "A","B","C","D"
Now see the Array members, execute $ArrayEg – see the result below:
Let’s now add item “E” to the Array – the command below will complete the task:
Unfortunately, trying to add an item to an existing array returns an error – see image below:
How to Resolve the PowerShell Add to Array Error
We resolve this error in 2 ways.
One, by using the System.Collections.ArrayList as shown below:
[System.Collections.ArrayList]$ArrayEgList = $ArrayEg
Now you can successfully execute the previous command that failed but this time using the created Array List, $ArrayEgList.
Next, execute $ArrayEgList to see that “E” was added successfully. See image below for the result of both commands:
Another way to add an ietm to an existing PS Array is to use the method illustrated in the command below:
$ArrayEg2 = $ArrayEg += "E"