Getting Started With Powershell Arrays

shutterstock_133287383The array is an essential data structure in programming. It is a collection of systematically arranged data items that can be addressed using indices. This allows you to store objects in them and perform different tasks on your data in methodical ways such as iterating through them, sorting them, searching for specific data and a lot more. Powershell provides methods to make these array tasks a lot more convenient.

Overview of Powershell 

Windows Powershell is a command-line shell and scripting language built on the .NET framework that is task-based. It was designed for system administration and provides full access to COM (Component Object Model) and WMI (Windows Management Instrumentations).

In Powershell, cmdlets (command-lets) are the ones responsible for administrating tasks that are supposed to be performed. These cmdlets are specialized .NET classes that implement particular operations.

New to Powershell? Learn more at Udemy

There are 129 standard cmdlets that are used for common system administration tasks like event logs, registry management, service management, process management etc. By using common syntax and naming conventions, cmdlets can easily share data without having to reformat or manipulate its contents.

Its interface is highly extensible. This way, independent software vendors or developers can build their own tools and utilities to manage their software.

Arrays in Powershell

When you want to be able to store and retrieve a set of data items in a systematic way, arrays are the way to go. In Powershell, the simplest way is to use a cmdlet that can return more than one object. The list that it returns can be stored in a variable. That variable is now an array.

Creating Arrays

In creating an array from scratch, all you need to do is to define your container variable and initialize it with values separated by commas:

$sampleArray = "A","B","C","a","b","c"

To initialize an array of integers:

$sampleArray = 1,2,3,4,5,6 or using the range operator (…)
$sampleArray = (1…6)

To initialize an array of elements with different data types:

$sampleArray = 1,2,”Hello”,”sample”,3,4,5

You can also declare an array explicitly by using:

[int []] $sampleArray = 1,2,3,4,5,6

If you wish to distribute the individual elements of your array into their own variables, just enumerate the containers separated by commas and equate them to your array like so:

$x,$y,$z = $sampleArray

So if sample array has 1,2,3 as its elements, x=1, y=2 and z=3

The nice thing about Powershell arrays is that they are mutable. If you wish to add new elements to your array apart from the ones you have initialized in the beginning, you just need to use the += operator. For example, if you have an array of 3 integers initialized as:

$sampleArray = 1,2,3

To add another integer: $sampleArray += 14

This adds the integer 14 to your 1, 2, 3 sequence.

Adding elements and initializing content is not the only capability and purpose of arrays. To actually work on the data in them, first you need to be able to retrieve the data.

Want to know more on Powershell? Take an online course at Udemy.com.

To retrieve an element, specify its index. The indices of an array start from 0.

$sampleArray

This returns all the elements in the array.

$sampleArray[0]

This returns only the first index of the array.

$sampleArray[6]

This returns the element in the 7th index of the array.

$sampleArray[0…5]

This returns all the elements of the array from index 0 thru index 5.

$sampleArray[-1]

This returns the last object in the array.

$sampleArray[0][0]

This returns the first element of the first row of a multidimensional array.

To know how many elements there are in an array, you can use the length function like so:

$sampleArray.length-1 //-1 because count starts from 0

So now you’ve gone through creating arrays and retrieving their data using different ways. But what if you need to access all the data one by one? Use them in an equation or simply print them on the screen? To do that, you will need to loop through them. You do that by following this syntax:

foreach ( $sampleItem in $sampleArray ) { $sampleItem }

Setting Values to Arrays

The values inside an array can’t all be the same throughout its lifetime. Of course, after data processing, there will always be a chance that you will need to change what is inside your arrays. To do that, you first need to access the index of the specific element you wish to update using the way which was taught to you earlier. After that, you need to use the assignment operator (=) to assign the new value to your specified index in the array:

$sampleArray[7] = 16

Another way to do it is by using the SetValue() method:

$sampleArray.SetValue(7,32)

The SetValue() method accepts 2 arguments. The first one is the index of the element that you wish to update. The second is the value that you wish to assign to that index.

You can merge or add the values of 2 or more arrays and put all their elements in one array container. To do so:

$sampleArray1 = 1,2,3
$sampleArray2 = 4,5,6
$mergedArray = $sampleArray1 + $sampleArray2

 

If you did not specify the data type of an array, by default it is considered as a basic object array. To find out the type of the array during runtime, use the gettype() method:

$sampleArray.gettype()

An array cannot just accept and accept values, especially when these values are not being used in the program anymore, because this is such a waste of memory. To maintain the integrity of your array, you will need to delete the ones that are not needed any longer or the ones that simply don’t belong in the array anymore. However, as easy as it is to add items or merge arrays in Powershell, there is no method to delete specific or individual array elements. To clean everything, you will need to delete the whole instance of the array (removing everything inside). If you wish to retain the rest of the elements, you will need to keep them somewhere else then reassign them excluding the element that you wished to remove. Here is the sample code of deleting a whole array:

Remove-Item variable:sampleArray.

Now that you have learned how to work with arrays in Powershell, you may now explore more of its libraries and perform even more complicated tasks using some or all of the methods and ways to make your array processing a breeze.