In VBScript I can do something like this
Set objDict = CreateObject("Scripting.Dictionary")
objDict.Add "item1", Array("data1", "data2")
objDict.Add "item2", Array("data3", "data4")
Then I can do a lookup using something like this
dataArray = objDict.Item("item2")
elem1 = dataArray(0)
elem2 = dataArray(1)
the result is elem1
contains "data3" and elem2
contains "data4"
I am not sure how to replicate this in PowerShell. Anyone help?
PowerShell Dictionary is also known as Hashtable or associate array is a compact data structure and composed of the key and the value pair.
Creating Ordered Dictionaries You can create an ordered dictionary by adding an object of the OrderedDictionary type, but the easiest way to create an ordered dictionary is use the [ordered] attribute. The [ordered] attribute is introduced in PowerShell 3.0. Place the attribute immediately before the "@" symbol.
@{} in PowerShell defines a hashtable, a data structure for mapping unique keys to values (in other languages this data structure is called "dictionary" or "associative array"). @{} on its own defines an empty hashtable, that can then be filled with values, e.g. like this: $h = @{} $h['a'] = 'foo' $h['b'] = 'bar'
GetEnumerator in PowerShell is used to iterate through the Hashtable or array to read data in the collection. GetEnumerator doesn't modify the collection data. Use foreach or foreach-object in PowerShell to iterate over the data read from the GetEnumerator.
In PowerShell, an array is simply a data structure that serves as a collection of multiple items. The items can be the same or different types. The proper way to create an array in PowerShell is by using @ ().
Definition of PowerShell Dictionary PowerShell Dictionary is also known as Hashtable or associate array is a compact data structure and composed of the key and the value pair.
Arrays in PowerShell can contain one or more items. An item can be a string, an integer, an object, or even another array, and one array can contain any combination of these items. Each of these items has an index, which always starts (sometimes confusingly) at 0.
When no data type is specified, PowerShell creates each array as an object array ( System.Object [] ). To determine the data type of an array, use the GetType () method. For example, to determine the data type of the $A array, type: PowerShell.
Dictionaries are called hashtables in PowerShell. When PowerShell first came out Microsoft also released a VBScript-to-PowerShell Conversion Guide, covering dictionaries and arrays among other things.
You define hashtables (dictionaries) like this:
$d = @{
'foo' = 'something'
'bar' = 42
}
Alternatively –for instance if you need to populate the hashtable dynamically– you can create an empty hashtable and add elements to it, like this:
$d = @{}
$d.Add('foo', 'something')
$d.Add('bar', 42)
or like this:
$d = @{}
$d['foo'] = 'something'
$d['bar'] = 42
Usually I prefer the latter, because it replaces existing keys instead of throwing an error.
$d1 = @{}
$d1.Add('foo', 23)
$d1.Add('foo', 42) # can't add another key with same name => error
$d2 = @{}
$d2['foo'] = 23 # key is automatically added
$d2['foo'] = 42 # replaces value of existing key
Arrays are defined as a simple comma-separated list:
$a = 'a', 'b', 'c'
Optionally you can also use the array subexpression operator:
$a = @('a', 'b', 'c')
That's useful if you need an array result, but are unsure of how many results your expression will produce. Without the operator you'd get $null
, a single value, or an array, depending on the result of the expression. By using the operator you always get an array, with zero, one, or more elements.
Both array and hashtable elements can be accessed via the index operator ([]
):
$d['foo'] # output: "something"
$a[1] # output: "b"
The elements of hashtables can also be accessed via dot-notation:
$d.foo # output: "something"
Of course you can nest arrays in hashtables and vice versa, just like you can do in VBScript. Your example would look somewhat like this in PowerShell
$dict = @{
'item1' = 'data1', 'data2'
'item2' = 'data3', 'data4'
}
$dataArray = $dict['item2']
$elem1 = $dataArray[0]
$elem2 = $dataArray[1]
PowerShell has learned a thing or two from other scripting languages, though. For instance you can assign arrays to a list of variables, so the above assignment of array elements to individual variables could be simplified to this:
$elem1, $elem2 = $dict['item2']
If the array has more elements than the number of variables on the left side of the assignment, the last variable takes the remainder of the array:
$x, $y, $z = 'a', 'b', 'c', 'd'
# $x -> 'a'
# $y -> 'b'
# $z -> @('c', 'd')
$x, $y, $z = 'a', 'b'
# $x -> 'a'
# $y -> 'b'
# $z -> $null
The index operator allows access to multiple elements:
$a = 'a', 'b', 'c', 'd', 'e'
$a[1,3,4] # output: "b", "d", "e"
as well as elements from the end of the array:
$a = 'a', 'b', 'c', 'd', 'e'
$a[-1] # output: "e"
$a[-2] # output: "d"
And the range operator (..
) allows you to get a slice from an array by just specifying the first and last index. It produces a list of numbers starting with the first operand and ending with the second operand (e.g. 2..5
→2,3,4,5
).
$a = 'a', 'b', 'c', 'd', 'e'
$a[1..3] # equivalent to $a[1,2,3], output: "b", "c", "d"
$a[-1..-3] # equivalent to $a[-1,-2,-3], output: "e", "d", "c"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With