top of page

PowerShell Variables & Arrays

Arrays

Arrays in PowerShell are used to store collections of items, such as strings, integers, or objects. They are versatile and can be manipulated in various ways to add, remove, or access elements.

 

Working with arrays in PowerShell:

​​

Declaring an Array is important and the only way to get desired results when

iterating through data. Example would be a foreach loop and assigning values.

 

$Services = Get-Service 

# Declaring an Array
$MyArray=@()
Foreach($Service in $Services){

     # Using the += To assign and append Array

     $MyArray+="Service Name: $($Service.Name) `t Status: $($Service.Status)"

 }

​

Creating Arrays

You can create arrays in PowerShell using the comma-separated

values enclosed in parentheses () or by using the @() notation:

​

# Using comma-separated values
$array1 = ("apple", "banana", "cherry")

# Using @() notation
$array2 = @("apple", "banana", "cherry")

 

Accessing Elements

You can access elements of an array by index. PowerShell

arrays are zero-indexed, meaning the first element is at index 0.

​

$array = "apple", "banana", "cherry"

# Accessing elements
$array[0]  # Output: apple
$array[1]  # Output: banana
$array[2]  # Output: cherry

 

Adding Elements

To add elements to an array, you can use the += operator or the += method.

​

$array = "apple", "banana"

# Using +=
$array += "cherry"

# Using +=
$array += "date", "elderberry"

$array  

# Output: apple, banana, cherry, date, elderberry

 

 

Iterating through Arrays

You can iterate through arrays using loops like foreach or for.

 

$array = "apple", "banana", "cherry"

# Using foreach loop
foreach ($item in $array) {
    Write-Output $item
}

# Using for loop
for ($i = 0; $i -lt $array.Length; $i++) {
    Write-Output $array[$i]
}

 

Removing Elements

To remove elements from an array, you can use the Remove()

method or other methods like slicing

​

($array[0..1]) or filtering ($array | Where-Object { $_ -ne "value" }).

$array = "apple", "banana", "cherry", "date", "elderberry"

# Using Remove()
$array.Remove("banana")

$array  

# Output: apple, cherry, date, elderberry
 

To keep from having issues

always declare Arrays!

In PowerShell, variables are used to store values that you can manipulate and use throughout your scripts and commands. Variables in PowerShell are always prefixed with the $ symbol. Here's a basic overview of how to use variables in PowerShell:

​

Declaring and Assigning Variables

To declare a variable and assign it a value, you simply use the $ symbol

followed by the variable name and then the assignment operator =:

# Declare a variable and assign a string value
$name = "John Doe"

# Declare a variable and assign a numeric value
$age = 30

# Declare a variable and assign an array value
$colors = @("Red", "Green", "Blue")

Using Variables

You can use variables in your PowerShell scripts and commands by referencing them with the $ symbol:

# Output the value of a variable
Write-Host $name

# Perform operations using variables
$sum = $age + 10
Write-Host "In 10 years, $name will be $sum years old."

# Accessing elements in an array
Write-Host "The first color is $($colors[0])"
 

Variables in PowerShell have different scopes which define where the variable can be accessed. The most common scopes are:

  • Global: Available throughout the entire session.

  • Local: Available only within the current scope, such as a function or script.

  • Script: Available throughout the entire script.​

You can define a variable's scope explicitly:

# Global scope
$global:globalVar = "This is a global variable"

# Local scope (default)
$localVar = "This is a local variable"

# Script scope
$script:scriptVar = "This is a script variable"
 

Example Usage

Here is a simple example that demonstrates declaring, using, and manipulating variables in a PowerShell script:

# Declare variables
$name = "Alice"
$age = 25
$colors = @("Red", "Green", "Blue")

# Use variables
Write-Host "Name: $name"
Write-Host "Age: $age"

# Perform calculations
$futureAge = $age + 5
Write-Host "$name will be $futureAge years old in 5 years."

# Access array elements
Write-Host "Favorite colors:"
foreach ($color in $colors) {
    Write-Host $color
}
 

This script declares several variables, outputs their values, performs a calculation, and iterates over an array. Understanding how to declare, assign, and use variables is fundamental to working effectively with PowerShell.

bottom of page