Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering Windows PowerShell Scripting

You're reading from   Mastering Windows PowerShell Scripting Master the art of automating and managing your Windows environment using PowerShell

Arrow left icon
Product type Paperback
Published in Apr 2015
Publisher
ISBN-13 9781782173557
Length 282 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Brenton J.W. Blawat Brenton J.W. Blawat
Author Profile Icon Brenton J.W. Blawat
Brenton J.W. Blawat
Arrow right icon
View More author details
Toc

Table of Contents (16) Chapters Close

Preface 1. Variables, Arrays, and Hashes FREE CHAPTER 2. Data Parsing and Manipulation 3. Comparison Operators 4. Functions, Switches, and Loops Structures 5. Regular Expressions 6. Error and Exception Handling and Testing Code 7. Session-based Remote Management 8. Managing Files, Folders, and Registry Items 9. File, Folder, and Registry Attributes, ACLs, and Properties 10. Windows Management Instrumentation 11. XML Manipulation 12. Managing Microsoft Systems with PowerShell 13. Automation of the Environment 14. Script Creation Best Practices and Conclusion 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:

Hashes

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:

Hashes

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:

Hashes

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:

Hashes

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:

Hashes

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.

You have been reading a chapter from
Mastering Windows PowerShell Scripting
Published in: Apr 2015
Publisher:
ISBN-13: 9781782173557
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image