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.

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:

Time Configuration for a Virtualized Domain Contro... Today we will talk about some of the features of time configuration on a virtualized domain controllers. Typically, the time synchronization scheme in...
Removing Old and Unused Drivers from Driver Store ... Each time you install or update your device driver, Windows OS (since Vista) continues to store the old version of the driver in the system Driver Sto...
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...
Installing WebDAV client on Windows Server 2016 WebDAV (Web Distributed Authoring and Versioning) is an extension of the HTTP/1.1 protocol. The WebDAV protocol allows to access files on the remote s...
Get-service: Checking the Status of Windows Servic... Using the Get-Service cmdlet you can get a list of all the services installed on the operating system, its status and startup type. This one and other...

Add Your Comment