Windows PowerShell 1.0 Hashtables

From Techotopia
Revision as of 20:16, 27 October 2016 by Neil (Talk | contribs) (Text replacement - "<table border="0" cellspacing="0"> " to "<table border="0" cellspacing="0" width="100%">")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
PreviousTable of ContentsNext
Working with Arrays in Windows PowerShell 1.0Basic Windows PowerShell 1.0 Operators


Purchase and download the full PDF version of this PowerShell eBook for only $8.99


Hash tables, also known as associative arrays, are arrays where each element consists of key-value pairs. Unlike regular arrays where elements are accessed using an index, the values in a hash table are accessed using the value's respective key.


Contents


Creating PowerShell Hashtables

Hash tables are represented in Windows PowerShell by the .NET System.Collections.Hashtable object and are created using key and value pairs enclosed in @{ and } characters. Each key is assigned a value using the '=' sign, and multiple pairs are separated by semi-colons (;):

PS C:\Users\Administrator> $book = @{ isbn = 12312312; title = "The Power of Widgets"; author = "Smith, John" }

In the above example, a hashtable is created with three values, each accessible using a key (isbn, title and author).

An empty hashtable may be created so that elements may be added at a later time as follows:

PS C:\Users\Administrator> $myhashtable = @{}

Accessing Hashtable Elements

<google>ADSDAQBOX_FLOW</google> All the entries of a hashtable may be accessed simultaneously simply by referencing the hashtable variable name:

PS C:\Users\Administrator> $book

Name                           Value
----                           -----
author                         Smith, John
isbn                           12312312
title                          The Power of Widgets

Unlike standard arrays, hashtable elements are accessed using the keys assigned at creation time rather than by an index value. The key can be used either with object property dot-notation as follows:

PS C:\Users\Administrator> $book.author
Smith, John

or using the square brackets ([]) array accessor notation, using the key rather than an index value. When using this technique it is important to note that the key must be enclosed in double quotes:

PS C:\Users\Administrator> $book["author"]
Smith, John

One advantage of this particular approach is that multiple elements may be extracted in a single command using a comma separated list of keys:

PS C:\Users\Administrator> $book["author","title"]
Smith, John
The Power of Widgets

Modifying Windows PowerShell Hashtable Elements

The value of a hashtable element may be changed using either object property dot-notation or array accessor techniques, the assignment operator (=) and the new value. The following commands change the values of the ISBN and title elements respectively:

PS C:\Users\Administrator> $book["isbn"] = 432234234
PS C:\Users\Administrator> $book.author = "Fred Wilson"
PS C:\Users\Administrator> $book

Name                           Value
----                           -----
author                         Fred Wilson
isbn                           432234234
title                          The Power of Widgets

Adding Elements to a Windows PowerShell Hashtable

New elements may be added to a hashtable using both object property and array accessor notation together with the new key and value pair. To add publisher and price elements to our $book hashtable, therefore, the following commands may be used:

PS C:\Users\Administrator> $book.publisher = "Pulp Media"
PS C:\Users\Administrator> $book["price"] = 10.34
PS C:\Users\Administrator> $book

Name                           Value
----                           -----
author                         Fred Wilson
publisher                      Pulp Media
price                          10.34
isbn                           432234234
title                          The Power of Widgets

Removing Elements from a Windows PowerShell Hashtable

Unwanted elements may be removed from a hashtable using the remove() method of the object, referencing the key associated with the element to be removed. For example, to remove the price element from our $book hashtable:

PS C:\Users\Administrator> $book.remove("price")
PS C:\Users\Administrator> $book

Name                           Value
----                           -----
author                         Fred Wilson
publisher                      Pulp Media
isbn                           432234234
title                          The Power of Widgets

Clearing All Elements from a Windows PowerShell Hashtable

In the event that all elements are to be removed from a Windows PowerShell hashtable, a fast alternative to removing each element one by one is to the use the clear() method as demonstrated below:

PS C:\Users\Administrator> $book.Count
4
PS C:\Users\Administrator> $book.Clear()
PS C:\Users\Administrator> $book.Count
0

Combining Hashtables

Elements in two or more hashtables may be combined using a variety of techniques. One important point to note is that the combination can only be performed by PowerShell if there are no duplicated keys in the hashtables. If duplicate keys are found the operation will fail.

The elements in one hashtable may be added to those in another hashtable using the += operator. In the following example, the elements in $book2 are added to $book1:

PS C:\Users\Administrator> $book1 += $book2

Alternatively, a new hashtable may be created comprised of the elements in multiple other hashtables using the + operator. For example, the following command creates a new hashtable named $book4 which contains copies of all the elements in the $book1, $book2 and $book3 hashtables:

PS C:\Users\Administrator> $book4 = $book1 + $book2 + $book3

Listing Hashtable Count, Keys and Values

The number of elements in a hashtable may be accessed via the count property:

PS C:\Users\Administrator> $book.Count
4

The keys contained within a hashtable can be listed at any time using the keys property:

PS C:\Users\Administrator> $book.keys
author
publisher
isbn
title

Similarly, the actual values may be listed through the use of the values property as illustrated below:

PS C:\Users\Administrator> $book.values
Fred Wilson
Pulp Media
432234234
The Power of Widgets

<google>BUY_WPS_BOTTOM</google>