Powershell Array Tutorial with Examples – POFTUT

Powershell Array Tutorial with Examples


Powershell arrays are used to store a collection of elements in a single variable. These elements do not to be the same type and declared in different ways.

Create Array

We will start with a simple example where we will create an array by providing the items. Items will be delimited with the comma. The array definition will start with a dollar sign $.

PS> $ myArr=1,"İsmail",3.5,"Ahmet"
Create Array
Create Array

We can see that different type of elements like integer, float, string is used to create an array.

Create Array with Range

We can create an array by providing a range with a start and end number. The intermediate values will be created automatically. We will use parenthesis and two points between the start and end number. In this example, we will create an array with a range from 1 to 10 like below.

PS> $myArr=( 1 .. 10 )
Create Array with Range
Create Array with Range

Create Numeric Array

We can create a numeric array by specifying the numeric array elements one by one. We will create a numeric array with even numbers like below.

PS> $myArr= 2, 4, 6, 8
Create Numeric Array
Create Numeric Array

Create Empty Array

It may sound a bit non-sense but we may need to create an empty array. We will use @() in order to create an empty array.

PS> $myArr=@()

Create Strongly Typed Array

As Powershell is a scripting language the provided element values will be interpreted accordingly. This may create some confusion during the creation of array type. We can create a strongly typed array by providing the array type like an integer. In this example, we will create a strongly defined integer array where the elements will be only integer values.

PS > [int[]] $myArr=10,20,30,40
Create Strongly Typed Array
Create Strongly Typed Array

Create Multi-Dimensional Array

As the problems become more complex we may need to use Multi-Dimensional arrays to express and solve complex problems with complex data types. We will use @ sign and parenthesis in order to create multi dimension arrays.

PS> $myArr= @((1,2,3),(4,5,6),(7,8,9))

Add Value To Array

One of the most used operations is adding an element or item to the existing array. We can use += or Add() function in order to add new element. For example, we will add 5 to the existing array.

PS> $myArr=10,20,30,40
PS> $myArr+=5
Add Value To Array
Add Value To Array

We can also use Add() function to add 5 to the existing array named myArr like below.

PS> $myArr.Add(5)

Print Specified Array with Index

As array elements are stored in an indexed way we can print specified item by providing its index. In this example, we will print the second item in the array myArr. Keep in mind that index starts from 0 in Powershell. So index 1 will be the second item in an array.

PS> $myArr[1]
Print Specified Array with Index
Print Specified Array with Index

Set or Change Specified Index Value

We can use index number in order to change given array specified element. In this case we will set index 1 as 100 like below.

PS> $myArr[1]=100
Set or Change Specified Index Value
Set or Change Specified Index Value

Retrieve Specified Range Items From Array

If we need to get a range of item from an array we can use .. and provide the start and end index values. In this example, we will get range between 2 and 4 .

PS> $myArr[2..4]
Retrieve Specified Range Items From Array
Retrieve Specified Range Items From Array

Return All Items Of Array

We can return all items of the given array in different ways the most basic way is just providing the array.

PS> $myArr
Return All Items Of Array
Return All Items Of Array

Get The Array Length

Arrays have length according to the count of the elements. We can get the array length information with the length option like below.

PS> $myArr.length
Get The Array Length
Get The Array Length

In this case our array named myArr is 4 .

Return Last Element Of Array

If we need to get the last element of the given array we will use the negative index which means start from the last position. We will use -1 like below.

PS> $myArr[-1]

Return Item From Multidimensional Array

As we can create multi dimensional array we may need to get an element. We will provide multiple indexes according to dimension count. For two dimension we will use  [][] like below.

PS> $myArr[1][1]

Check If Given Element Exist

We can use -eq to check whether an array has specified item value. In this example, we will check if myArr has the value 10 like below.

PS> $myArr -eq 10
Check If Given Element Exist
Check If Given Element Exist

LEARN MORE  Random Number Generator In Python

Leave a Comment