Home ยป PowerShell Arrays

PowerShell Arrays

PowerShell arrays is a data structure providing a structured way to store and manipulate collections of items. These items can be of the same type or different types stored sequentially in memory.

An array can hold various types of data, including strings, integers, objects, and even other arrays. They provide a convenient way to organize and access related data elements.

Creating Arrays

In PowerShell, array can created using various methods including @(), New-Object or casting.

Using Literal Declaration @() to Create Array

$languages = @("Python","PowerShell","Rust","Java")
$languages
Python
PowerShell
Rust
Java

Using the New-Object cmdlet to Create Array

$languages = New-Object System.Collections.ArrayList
$languages.Add("Python")
$languages.Add("PowerShell")
$languages.Add("Rust")
$languages.Add("Java")

$languages
Python
PowerShell
Rust
Java

By Casting to Create Array

By casting to [array] type, you can create an array in PowerShell.

$fruits = [array]@("Apple", "Banana", "Orange")
$fruits
Apple
Banana
Orange

Accessing Array Elements

Array elements in PowerShell are accessed using the index notation, starting from 0 for the first element.

$languages = @("Python","PowerShell","Rust","Java")

Write-Host "First Language: $($languages[0])"
First Language: Python

Manipulating Arrays

PowerShell provides several methods for manipulating arrays:

Adding Elements

# Add a new element with value to the array using + operator
$languages += "Go"

Write-Host "Elements in the Array: $languages"
Elements in the Array: Python PowerShell Rust Java Go

Changing Value in Arrays

# Change the programming language in the array using the index
$languages[0] = "Javascript"
$languages
Javascript
PowerShell
Rust
Java
Go

Slicing Arrays

Slicing arrays allows you to create a new array containing only the elements within the specified range.

For example, in the given below example, to extract a subset of $languages array, say elements from 0 to 1, we can slice the array as follows.

$langs =  $languages[0..1]

$langs
Javascript
PowerShell

Joining Arrays

You can merge two arrays using the + operator.

$languages = @("Python","PowerShell","Rust","Java")
$scriptlanguages = @("VB Script", "JavaScript")

$langs = $languages + $scriptlanguages
$langs
Python
PowerShell
Rust
Java
Go
VB Script
JavaScript

Cool Tip: How to use multidimensional array in PowerShell!

Looping through Arrays

You can use for loop to iterate through an array elements. Use the index to access the elements.

$languages = @("Python","PowerShell","Rust","Java")

for ($i = 0; $i -lt $languages.Length; $i++) {
    Write-Host $languages[$i]
}
Python
PowerShell
Rust
Java

Cool Tip: How to use hashtables in PowerShell!

Conclusion

I hope the above article on how to create array in PowerShell and manipulate them using several methods is helpful to you.

You can find more topics about Active Directory tools and PowerShell basics on the ActiveDirectoryTools home page.