Changes

Jump to: navigation, search

Windows PowerShell 1.0 Hashtables

5,220 bytes added, 19:43, 19 November 2008
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...
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.

== 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 (;):

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

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:

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

== Accessing Hashtable Elements ==

All the entries of a hashtable may be accessed simultaneously simply by referencing the hashtable variable name:

<pre>
PS C:\Users\Administrator> $book

Name Value
---- -----
author Smith, John
isbn 12312312
title The Power of Widgets
</pre>

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:

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

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:

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

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

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

== 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:

<pre>
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
</pre>

== 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:

<pre>
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
</pre>

== 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:

<pre>
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
</pre>

== 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:

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

== Listing Hashtable Count, Keys and Values ==

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

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

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

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

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

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

Navigation menu