Array And Hash Tables In PowerShell
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed break, continue, and exit statements in PowerShell.
https://cloudaffaire.com/break-continue-exit-in-powershell/
In this blog post, we will discuss Array and Hash Tables in PowerShell. Like any other scripting language, PowerShell also supports array. An array is a data structure consisting of a collection of elements (values or variables), each identified by at least one array index or key. An array is stored such that the position of each element can be computed from its index tuple. The simplest type of data structure is a linear array, also called a one-dimensional array. PowerShell also supports the use of the multi-dimensional array. An array can contain a collection of different data types.
Array And Hash Tables In PowerShell:
Create An Array In PowerShell:
In PowerShell, you can create and initialize an array by assigning multiple values separated by a comma to a variable. Range operators can also be used to initialize an array. PowerShell also supports the use of sub-expression operator @(..) to create an array. You can also store the output of a cmdlet to an array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
######################## ## PowerShell | Array ## ######################## ## PowerShell Latest Version (5) ## -------------------- ## array initialization ## -------------------- $a = 1,2,3,4,5 ## define the array with values $a ## returns 1,2,3,4,5 $a.GetType() | Select-Object BaseType ## returns "System.Array" $a = 1..5 ## define the array with values $a ## returns 1,2,3,4,5 $a.GetType() | Select-Object BaseType ## returns "System.Array" $a = @(1,2,3,4,5) ## define the array with values $a ## returns 1,2,3,4,5 $a.GetType() | Select-Object BaseType ## returns "System.Array" $a = 1,"hello",2,"world" ## array can contain multiple datatypes $a ## returns 1,"hello",2,"world" $a.GetType() | Select-Object BaseType ## returns "System.Object" $a = (Get-PSDrive).Name -match '^[a-z]$' ## define an array using cmdlet output $a ## returns all the drive letters $a.GetType() | Select-Object BaseType ## returns "System.Object" |
Get Array Elements In PowerShell:
Each array elements have an associated array index with it and you can retrieve individual or range of array elements using the subsequent array index. Without the index number, the entire array elements get printed. An array index starts with 0. You can also use a negative index (starts with the end of the array) to retrieve array elements.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
## ----------------------- ## array elements retrieve ## ----------------------- ## retrieve array elements $a = 1..10 ## define the array with values $a[0] ## returns 1 $a[6] ## returns 7 $a[-1] ## returns 10 $a[2,4,6] ## returns 3,5,7 $a[3..6] ## returns 4,5,6,7 $a[-3..-8] ## returns 8,7,6,5,4,3 $a[2..-2] ## returns 3,2,1,10,9 $a[2..4+6..8] ## returns 3,4,5,7,8,9 ## iterations over array elements $a = 1..10 ## define the array with values $a[0..9] ## using range opeartor foreach ($i in $a) ## using foreach loop { $i } for ($i = 0; $i -le ($a.length - 1); $i++) ## using for loop { Write-Host "a[$i] ==> " $a[$i] } $i = 0 ## using while loop while ($i -le ($a.length - 1)) { Write-Host "a[$i] ==> " $a[$i] $i++ } |
Create A Multi-dimensional array in PowerShell:
PowerShell also supports the use of a multi-dimensional array. A multi-dimensional array is basically an array of array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
## ----------------------- ## multi-dimentional array ## ----------------------- $a = @(@(1..3),@(4..6),@(7..9)) ## decalre a multi-dimentional array ## 1 2 3 ## 4 5 6 ## 7 8 9 $a[0] ## returns 1,2,3 $a[2] ## returns 7,8,9 $a[0][0] ## returns 1 $a[2][1] ## returns 8 $a ## returns 1,2,3,4,5,6,7,8,9 |
Array Manipulation In PowerShell:
An array has multiple methods and properties that can be used to manipulate an array. You can get all the methods and properties of an array using Get-Member cmdlet.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
## ------------------ ## array manipulation ## ------------------ $a = 0..9 ## define an array with values ,$a | Get-Member ## get all methods and properties available for array manupulation ## array properties $a.Length ## returns 10 $a.Count ## returns 10 $a.Rank ## returns 1 $a.ForEach({$_ * 10}) ## returns 0,10,20,30,40,50,60,70,80,90 $a.Where{ $_ % 2 } ## returns 1,3,5,7,9 $a.Where({ $_ % 2 }, 'Default', 3) ## returns 1,3,5 $a.Where({ $_ % 2 }, 'Last', 3) ## returns 5,7,9 $a.Where({ $_ % 2 }, 'SkipUntil', 4) ## returns 1,2,3,4 $a.Where({ $_ % 2 }, 'Until') ## returns 0 $a.Where({ $_ % 2 }, 'Split') ## splits in even and odd $a.Clear() ## clears all array values Remove-Variable -Name "a" ## array methods [System.Collections.ArrayList]$a = 0..5 ## creates a arraylist $a.GetType() | Select-Object Name ## returns ArrayList $a.Add(6); ## add 6 to the end of the array $a ## returns 0,1,2,3,4,5,6 $a.Insert(3,10) ## inserts new item 10 at index position 3 $a ## returns 0,1,2,10,3,4,5,6 $a.Remove(10) ## remove any items having value 10 $a ## returns 0,1,2,3,4,5,6 $a.RemoveAt(1) ## remove index item 1 $a ## returns 0,2,3,4,5,6 $a.IndexOf(3) ## returns 2 |
Associative Array (Hash Table) In PowerShell:
A hash table, also known as a dictionary or associative array, is a compact data structure that stores one or more key/value pairs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
## ------------------------------- ## associative array or hash table ## ------------------------------- ## initialization $h = @{1 = "a"; 2 = "b"; 3 = "c"} ## initialize a new hash table $h ## returns hash table data $h.GetType() | Select-Object Name ## returns "Hash table" $h = [ordered]@{1 = "a"; 2 = "b"; 3 = "c"} ## initialize an ordered hash table $h ## returns hash table data $h.GetType() | Select-Object Name ## returns "OrderedDictionary" ## retrieval $h = @{key1 = "a"; key2 = "b"; key3 = "c"} ## initialize a new hash table $h ## returns entire hash table $h.keys ## returns all keys $h.values ## returns all values $h.key1 ## returns value based on key $h["key1"] ## returns value based on key ## manupulation $h.Add("Key4","d") ## adds a new key-value $h $h["key4"] = "x" ## update existing key value $h $h.Remove("key4") ## remove existing key and value $h $h.Clear() ## remove all key and value $h Remove-Variable -Name "h" ## delete hash table |
Hope you have enjoyed this article. In the next blog post, we will discuss Functions in PowerShell.
To get more details on PowerShell, kindly follow below official documentation