PowerShell: Array
What is Array
- Array is a list of values.
- The values can be different types.
- Array length is fixed.
- You can add items or join array, but PowerShell creates a new array, each time, and is slow.
Create Array
Array Sub-Expression Operator
$x = @(3, 4, 5) Write-Host $x
Comma Operator
array can be created by values separated by comma
$x = 3, 4, 5
can be multiple lines:
$x = "a", "b", "c";
🛑 WARNING: if you format array items one per line, the last item must not have comma.
Create a Array of Single Item
To create a array of single item, precede the item by a comma.
$a = ,4
Create Array (Range Operator)
min..max
-
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 # 3
PowerShell. Array
- PowerShell: Array
- PowerShell: Array Sub-Expression Operator, Collection to Array
- PowerShell: Array and Types
- PowerShell: Nested Array, Multi-Dimensional Array
- PowerShell: Array, Get Item
- PowerShell: Array, Set Item
- PowerShell: Test If Collection Contains a Value
- PowerShell: Join Array, Append
- PowerShell: Filter Array (Where-Object)
- PowerShell: Delete Array, Clear Array
- PowerShell: Array to String
- PowerShell: Array Methods
- PowerShell: Iterate Array