Windows PowerShell 1.0 Hashtables

From Techotopia
Revision as of 19:43, 19 November 2008 by Neil (Talk | contribs) (New page: 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 i...)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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

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 acessor 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, an 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

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