PowerShell: Array

By Xah Lee. Date: . Last updated: .

Create Array (the comma operator)

it can be created by values separated by comma

$x = 3, 4, 5

items can be different types:

$x = 3, "abc", 5

can be multiple lines:

$x =
"a",
"b",
"c";

🛑 WARNING: Last item must not have comma. And best to add a SEMICOLON at the end. This is a frequent error if you write PowerShell script.

Create a Array of Single Item

To create a array of single item, precede the item by a comma.

$a = ,4

or use the PowerShell: Array Sub-Expression Operator @(), Collection to Array

$x = @(4)

Create Array by Range Operator

min..max

(the range operator.) Creates an array of contiguous integers, from min..max, inclusive.

$a = 3..7
# result 3 4 5 6 7

Create Array of All 0

# create a array of 0
$x = [int[]]::new(3)
Write-Host $x
# 0 0 0

length

use method length or its alias count

(3,4,5).length -eq 3

PowerShell: Array