PowerShell: Array
What is Array
- Array is a list of values.
- The values can be the same type or different types.
- PowerShell array length is fixed. To remove item or add item is expensive.
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: 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
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
- 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
- PowerShell: Delete Array, Clear Array
- PowerShell: Array to String
- PowerShell: Array Methods
- PowerShell: Iterate Array