Variables and Arrays

Variables and Arrays in PowerShell: All variables in PowerShell are prefixed with a dollar sign .

All variables in PowerShell are prefixed with a dollar sign $. They are weakly typed by default, but can be strongly typed (casting) if needed.

# Basic Assignment
$name = "Administrator"
$age = 35

# Strongly Typed Variables
[int]$port = 8080
[string]$serverIP = "192.168.1.100"

# Arrays
# Created using an @ symbol and parentheses. Elements separated by commas.
$servers = @("Server1", "Server2", "Server3")
$mixed = @(1, "Text", $true)

# Appending to an array (Creates a new array under the hood)
$servers += "Server4"

# Indexing (0-based)
Write-Output $servers[0]   # Server1
Write-Output $servers[-1]  # Server4 (Negative indexing gets last element!)