Powershell Array: Uses and examples

Powershell Array: Uses and examples

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.

How to create a PowerShell array

Creating a PowerShell array is as simple as creating a variable and adding items into the variable separated by commas (,). To create a PowerShell array called ‘$ArrayEg’, execute the command bellow:

$ArrayEg = "A, B, C, D"

This is very helpful when scripting. I used this when I was creating the lab used in my PowerShell Tutorial book series. I needed to add some Active Directory users to a set of security groups.

To proceed, I added the group names in an array called $Groups as shown below:

$Groups = "Group1, Group2, Group3"

Once I had the group names in an array, it was easy to iterate through the array items using a ForEach loop. Using the ForEach loop, I called each item of the array (the groups) and used Add-ADGroupMember to add users to the groups as required. Smart, isn’t it!

Manipulating a PowerShell array

A PowerShell 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, execute the command below:

$Groups.GetType()

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. I will provide additional examples later in this article.

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:

powershell-array-image-1

 

Notice that in the figure, 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.

Powershell methods and properties are covered in detail in my book PowerShell Tutorial. To learn more about object properties, methods and other PowerShell scripting technics, get a copy of PowerShell Tutorial Volume 1. You may also refer to the references at the end of this page.

How to access the items in a PowerShell array

We defined a PowerShell 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; execute the command below:

$ArrayEg = "A, B, C, D"

To list the content of the array, simply execute $ArrayEg as shown below:

$ArrayEg

The previous command will list the values A, B, C, D in that order. There might be circumstances where you need to access on of the items contained in the array.

Before I 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, run the command below:

($ArrayEg)[0]

The command returns ‘A’ as shown below:

powershell-array-image-2

 

To return the second item, ‘B’, replace ‘0’ with ‘1’ in the previous command.

Practical example of a PowerShell array

I have used the PowerShell array a number of times. I already mentioned how I used it in the script for my PowerShell Tutorial book series. I will now give one more example of the application of a PowerShell array .

PowerShell array application in reporting top CPU processes

In this example, I will demonstrate how to use the knowledge about PowerShell array to find the process that has the top CPU utilisation 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 variable. To see the information stored, execute the command below.

$Proc

The result of the previous command is shush below.

powershell-array-image-3

 

Notice the CPU(s) column? This column shows the CPU consumption of each process.

How to find the process with the highest CPU consumption

To proceed, we need to sort the process in order of CPU consumption. To achieve this, we pipe the $Proc PowerShell array to the Sort-Object cmdlet. The Sort-Object cmdlet sorts the content of the PowerShell array in ascending order.

$Proc | Sort-Object CPU

Unfortunately the previous command did not sort the items in the order we want them. See the result of the previous command below. We will like to have the items in descending order.

powershell-array-image-4

 

Sorting the items in descending order is very simple. It requires us to include a parameter of the Sort-Object cmdlet. The parameter we require is the ‘Descending’ parameter. See the amended command below.

$Proc | Sort-Object CPU -Descending

powershell-array-image-5

 

Notice that the items are now sorted in order of CPU consumption. The items are now sorted with the process with the highest CPU on top. At this point, we can return the process with top CPU consumption. The command below does the magic!

($Proc | Sort-Object CPU -Descending)[0]

powershell-array-image-6

 

The will return the first item in the PowerShell array. This is the process with top CPU consumption. How good is that!

References and further reading

Leave a Reply