array arr powershell cover

Arrays Basics on PowerShell

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:


array powershell arr

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:


array powershell arr length

If you need to increase the length of the array, you can use the + or += operators:


array arr powershelll

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.

READ ALSO  How to transfer FSMO Roles From a Failed Domain Controller

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

arr powershelll

To delete an array, use the Remove-Item cmdlet (del alias) and remove the array from the virtual disk named variable.

Del variable:arr3

array powershell arr 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

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

system array powershell

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

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

arrays arr powershelll 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 = @()

You may also like:

How to transfer FSMO Roles From a Failed Domain Co... In case domain controller, which owns FSMO (Flexible Single Master Operation) roles, is fail (virus attack, fatal software problems or catastrophic ha...
How to transfer FSMO Roles using PowerShell When you create a domain, all FSMO roles assigned to the first domain controller in the forest by default. You can transfer FSMO roles from one DC to ...
Guide to SQL Server Transaction Log Backup In case you use Microsoft SQL Server you might heard about the Simple and Full database recovery models. Probably you also know that a Simple recovery...
Accessing Hyper-V Virtual Machine Console Using RD... Remote Desktop Connection Manager (RDCMan) is an official utility from Microsoft to connect to servers via RDP (which replaced the Remote Desktops sna...
Move FSMO Roles and Upgrade Domain to Windows Serv... In this article we will show you how to promote a new domain controller with Windows Server 2016 in the Active Directory domain, move FSMO roles from ...