Book Image

Mastering Windows PowerShell Scripting

By : Brenton J.W. Blawat
Book Image

Mastering Windows PowerShell Scripting

By: Brenton J.W. Blawat

Overview of this book

Table of Contents (22 chapters)
Mastering Windows PowerShell Scripting
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Hashes


Hashes are used like arrays. The main difference is that they use the values as indexes versus sequentially numbered indexes. This provides easy functionality to add, remove, modify, and find data contained in the hash table. Hash tables are useful for static information that needs a direct correlation to other data:

Name

Value

John.Doe

Jdoe

Jane.Doe

jdoe1

A good example of a hash table would be in the instance of an Active Directory migration. In most Active Directory migrations, you would need to correlate old usernames to new usernames. The preceding table represents a username mapping table for these types of migrations. While a traditional array would work, a hash table makes this much easier to do.

To create the preceding hash table, enter the following command:

$users = @{"john.doe" = "jdoe"; "jane.doe" = "jdoe1"}
$users

The output of this is shown in the following screenshot:

After you create the table, you may want to find a specific user. You can search a hash table by using the hash's indexing function. This is done by calling $hashName["value"]. An example of this would look like the following command:

$users = @{"john.doe" = "jdoe"; "jane.doe" = "jdoe1"}
$users["john.doe"]

The output of this is shown in the following screenshot:

After entering the command, you will see that $users["john.doe"] returns jdoe as the correlating value in the hash.

One of the most popular methods to use with hash tables is the add method. The add method allows you to enter new values within the hash table. You can use this while building the hash table, as most hash tables are built within a script. If you want to add another user to the hash table, use the add method as shown here:

$users = @{"john.doe" = "jdoe"; " jane.doe" = "jdoe1"}
$users
$users.add("John.Smith", "jsmith")
$users

The output of this is shown in the following screenshot:

You will see that John.Smith with the value of jsmith is now added to the hash table.

You can also update values in a hash by leveraging the hash's index. This is done by searching for a value and then setting its correlating hash value equal to a new value. This looks like $arrayName["HashIndex"] = "New value". An example of this is given here:

$users = @{"john.doe" = "jdoe"; "jane.doe" = "jdoe1"}
$users
$users["jane.doe"] = "jadoe"
$users

The output of this is shown in the following screenshot:

You will see that the mapped value for Jane.Doe now reads jadoe. This is vastly different from an array, where you would have to search for a specific value location to replace the value.

If you want to remove a user from the hash table, use the remove method, as shown here:

$users = @{"john.doe" = "jdoe"; "jane.doe" = "jdoe1"}
$users
$users.remove("Jane.Doe")
$users

The output of this is shown in the following screenshot:

You will see that Jane.Doe is now removed from the hash table. This method is helpful when you need to remove specific values, which meet certain criteria, from the hash table.