Array is a data structure to store a collection of items, these items can same or different data type.
Initalizing and Defining an Array
To define and initilialize an array by simply assigning multiple values into a variable. The values delimited by comma. The following some examples how to define an array.
If there are not data type specified then the array will be treated as object array type System.Object[]. To get data type information we simply use GetType() method.
Result with no data type specified.
# define array of 9 numeric values
$arr = 2, 34, 5, 7, 1, 2, 9, 90, 100
# you can use range operator (..) to assign $number 1-10
$number = 1..10
# array of string
$words = "I", "Like", "to", "Learn", "PowerShell"
# different type
$mix = "123", 123, $(Get-Date), $(dir)
If there are not data type specified then the array will be treated as object array type System.Object[]. To get data type information we simply use GetType() method.
$arr = 2, 34, 5, 7, 1, 2, 9, 90, 100
Write-Host 'Type of $arr : ' $arr.GetType()
$words = "I", "Like", "to", "Learn", "PowerShell"
Write-Host 'Type of $words : ' $words.GetType()
Result with no data type specified.
Type of $arr : System.Object[]
Type of $words : System.Object[]
To specify data type in array by simply doing it like this.
int[]]$arr = 2, 34, 5, 7, 1, 2, 9, 90, 100
Write-Host 'Type of $arr : ' $arr.GetType()
[string[]]$words = "I", "Like", "to", "Learn", "PowerShell"
Write-Host 'Type of $words : ' $words.GetType()
The script will produce this output
Type of $arr : System.Int32[]
Type of $words : System.String[]
see PowerShell data types for details.
$arr = @(1, 2, 3)
$words = @("hello", "world")
$emptyArray = () # this will generate an error
$emptyArray = @()
Accessing Array Elements
Indices in array start from 0, so 0 is the first element, 1 is the second and so on. Suppose we have an array $colors.
$colors = 'Black', 'White', 'Yellow', 'Blue', 'Green'
$colors[0] # refer the first element
$colors[2] # refer the third element
$colors[0..2] # refer the element at index 0, 1, and 2
$colors[-1] # refer the last element
$colors[-2] # refer the second last
$colors[-1..-3] # refer to Green, Blue, and Yellow
$colors[-1..-$colors.Count] # refer the entire element in reverse order
You can also use plus operator (+) to combine a ranges with list of elements in an array. Example
$arr = @(1..12)
$arr[1,3 + 4..7] # print element index 1, 3, 4 through 7
$arr[0..2 + 3..5] # print element index 0 through 2 and 3 thorugh 5
PowerShell also support multidimensional array.
$arr = @(1, 2, 3), @(4, 5, 6), @(7, 8, 9)
Write-Host $arr[0] # print 1 2 3
Write-Host $arr[0][0] # print 1
Write-Host $arr[1][1] # print 5
Manipulating in Array
Things that you can manipulate in powerShell arrays are update the element, add new element, and delete element.
$colors = 'black', 'orange', 'blue', 'yellow'
$colors[1] = 'green' # update second element to 'green'
$colors[2] = 'gray' # update third element to 'gray'
$colors = $colors + 'brown' # add 'brown' to array
$colors += 'orange' # use operator assignment instead
$colors[3] = $null # delete 'yellow' from array
# multi-dimensional array
$arr = @(1, 2, 3), @(4, 5, 6), @(7, 8, 9)
$arr[0][0] = 100 # 1 --> 100
$arr[1][2] = 600 # 6 --> 100
$arr += @(10, 11, 12)
$arr[0] = $null # delete 1 2 3
Methods of Arrays
Besides you can use $null to delete elements in arrays you can also use Clear() method.
$colors = 'black', 'orange', 'blue', 'yellow'
Write-Host 'Before delete:' $colors
$colors = $null
Write-Host 'After delete: ' $colors
# you can use Clear()
$colors = 'black', 'orange', 'blue', 'yellow'
Write-Host 'Before delete:' $colors
$colors.Clear()
Write-Host 'After delete: ' $colors
You can use ForEach() to iterate each element in array.
$arr = @(1..4)
$arr.ForEach({ $_ * $_ })
@("11/6/1994", "11/7/1994").ForEach([datetime])
You can either use Where method.
$arr = @(1..10)
$arr.Where{$_ % 2 -eq 0} # print even number in $arr
$arr.Where{$_ % 2} # print odd number in $arr
By the way $_ is one of Automatic variables.
0 Response to "PowerShell Arrays"
Post a Comment