Arrays are used very often when creating PowerShell scripts, so you need to understand how to work with them. In this article we’ll take a look at the basic examples of using arrays in PowerShell.
Usually an array is a set of elements arranged one after the other in a certain order. Each element of the array has its own sequence number. To access the desired array element, you need to specify its number (index).
Let’s create a simple array with three elements:
$arr1 = "one", "two", "three";
To add a new element to an array, run the following command:
$arr1 = $arr1 + "four"
Let’s display the contents of this array:
You can get the number of array elements using the Length property:
If you need to access a specific element of an array, you must specify its sequence number when accessing it. The index of the elements of the array begins with 0. To refer to the first element of the array $arr1, use the command:
You can also specify a negative value for the sequence number. So, for example, [-1] means the last element of the array:
The last element of the array is also numbered as Length-1 (which is logical, since the numbering starts from zero). This is convenient, since you don’t need to look at the total number of array elements, but simply select a range, for example:
If you need to increase the length of the array, you can use the + or += operators:
In fact, in this case we did not add elements to the existing array, but created a new one into which the contents of the old array were copied and new elements were added.
By the same logic, we can’t remove elements from an already existing array, but we can create a new array and copy all the elements there, except unnecessary ones:
$arr2=$arr1[0,1 + 4..($arr.Length-1)]
It is quite easy to combine arrays:
$arr3=$arr1 + $arr2
To delete an array, use the Remove-Item cmdlet (del alias) and remove the array from the virtual disk named variable.
Each element of the array can have its own type. When working with arrays, remember that by default PowerShell tries to determine the type of data by itself. For example, create a new variable and put a few numbers into it, and then check the data type in the variable:
$arr2 = 1, 2, 3 $arr2.GetType()
As you can see, to create an array we need to assign values to its elements and get an array (System.Array type). But if you put one value into a variable, then PowerShell will not create an array:
$notarr = 1 $notarr.GetType()
As the example shows, PowerShell determines the type of data in a variable, depending on the number of objects. To create an array consisting of one element:
$arr4 = ,1 $arr4.GetType()
By default, all arrays in PowerShell are polymorphic, i.e. can contain elements of different types. If necessary, you can restrict the members of the array to a specific data type (a typed array). For example, you can only specify integer values as array elements:
[int32]$arr5 = 1
And in this way we can define system process as array members:
[System.Diagnostics.Process]$arr6 = Get-Process
You can also create an array with the help of the subexpression operator of the array “@”, which forms the array in any case (even with complete absence of objects). It is very convenient to first initialize the array variable and then add the necessary objects to it without worrying about their number:
$arr0 = @()