EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell Dictionary

PowerShell Dictionary

Updated March 6, 2023

PowerShell Dictionary

Definition of PowerShell Dictionary

PowerShell Dictionary is also known as Hashtable or associate array is a compact data structure and composed of the key and the value pair. Hashtable is a .Net namespace System.Collections.Hashtable while the dictionary is from a .Net namespace called Systems.Collection.Specialized.OrderedDictionary and it’s the order of the output that differentiate them, ordered dictionary has the ordered output while the Hashtable doesn’t have the ordered output.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Syntax:

Syntax for Hashtable:

@{ <name> = <value>; [<name> = <value> ] ...}

The syntax for Ordered Dictionary:

[ordered]@{ <name> = <value>; [<name> = <value> ] ...}

This [Ordered] attribute was introduced in PowerShell v3.0.

When we create a Hashtable or Dictionary, it starts with the at-sign (@) and is surrounded by the curly braces {}.
Inside the braces, we define the key and values. Value is assigned to the key using the equal (=) symbol and multiple key values are separated using a semicolon (‘;’).

If the hashtable is in the ordered format (dictionary) then [Ordered] should be written ahead of it.

How does the dictionary work in PowerShell?

PowerShell dictionary or the hashtables are the same in manner but the only difference is Dictionary produces the ordered output while the Hashtable output is not in the order.
You can create an empty hashtable using the below command,

$htable = @{}

When you check the type of the hashtable, its base type is a System. Object and it is the hashtable.

$htable.GetType()

Output:

PowerShell Dictionary 1

We can simply insert values to the hash table by adding the keys and their values.

$htable = @{Name='Chirag';EmpID="001";City="Pune"}
$htable

Output:

PowerShell Dictionary 2

Once the hashtable is created and if you want to create an ordered dictionary from it you can’t directly cast a hash table, it will throw an alert.

[Ordered]$htable

Output:

DB2 Create Table 3

Even if we create a new empty hash table and assign and cast the hash table to it, it won’t work.

$dict = @{}
$dict = [ordered]$htable

Output:

PowerShell Dictionary 3

To create a dictionary, we initially need to cast the [Ordered] hashtable when we assign values as shown below.

$htable = [Ordered]@{Name='Chirag';EmpID="001";City="Pune"}

Output:

PowerShell Dictionary 4

The Output will appear in the order we assign values to them. We can reverse typecast here. It is possible.

$hash = [hashtable]$htable

Output:

PowerShell Dictionary 5

There are also a few methods and properties supported by Hashtable or the dictionary to perform the operation or to retrieve the data from the hashtable.

$htable | Get-Member

Output:

PowerShell Dictionary 6

Examples

Lets us discuss examples of PowerShell Dictionary

Example #1: Create a Hashtable and adding values.

This example creates a service table that stores information about a particular service.

$servicetable = @{Name='Spooler'; Starttype='Manual'; Status='Disabled'}
$servicetable

Output:

example 1

The output is not in the ordered format as input so we create a dictionary (ordered hashtable).

$servicetable = [Ordered]@{Name='Spooler'; Starttype='Manual'; Status='Disabled'}
$servicetable

Output:

example 1-1

Throughout other examples, we will use this created hashtable.

Example #2: Retrieving Hashtable values and keys.

To get the hashtable keys, we need to select its property Key as shown below.

$servicetable.Keys

Output:

example 2

To retrieve hashtable values,

servicetable.Values

Output:

example 2-1

To retrieve the particular value from the Key, we can provide the key name.

$servicetable.Name

Output:

example 2-2

Example #3: Adding new Keys and Value to the Hashtable

To add a new key and associated values to the hashtable, we can use the following method.

$servicetable += @{DisplayName='Print Spooler'}
$servicetable

Output:

example 3

The above one is an explicit method, we can also use Add() method as well.

$servicetable.Add("CanStop","True")
$servicetable

Output:

example 3-1

Example #4: Deleting Hashtable Key

To Remove the hashtable key, we need to use the Remove() method. We just need to provide Key inside the Remove() function.

$servicetable.Remove("CanStop")
$servicetable

Output:

example 4

Example #5: Modify the Hashtable key value.

To modify the specific hashtable’s key value, we just need to access the key and assign the value to it.

$servicetable.Status = "Stopped"
$servicetable

Output:

example 5

Example #6: Assign multiple values to the hashtable key.

We can’t directly assign multiple values to the hashtable key because the individual key is considered a string as shown below.

$servicetable.Name += "Winrm"
$servicetable

Output:

example 6

To solve this we initialize the particular key as an array and then create multiple values but the problem is if we initialize the key as an array after creating a hashtable its existing value will be wiped out and we need to create it again.

$servicetable.Name = @()
$servicetable

Output:

example 7

We need to add values now.

$servicetable.Name += "Spooler"
$servicetable.Name += "WinRM"
$servicetable

Output:

example 6-1

Name property will give multiple values now.

$servicetable.Name

service name

Example #7: Converting string data to the hashtable.

To convert the string output to the hashtable, we can use the ConvertFrom-StringData command.

$stringdata = @"
Book = The Owl
Genre = Fiction
Author = Greg Truth
"@

Output:

example 7-1

To convert to the hashtable,

$stringdata | ConvertFrom-StringData

Output:

example 7-2

Example #8: Hashtable with Object Types.

In this method, we are storing Objects (which have multiple keys and values) in the Value field.

$ObjectHash = @{
'PowerShell'= (Get-Process notepad++)
'Winrm' = (Get-Service WinRM)
}

Output:

example 8

When we check the individual key’s value, we can use the value as a separate hashtable as shown below.

$ObjectHash.PowerShell

PowerShell Dictionary 8

$ObjectHash.winrm

PowerShell Dictionary 9

Further expanding,

$ObjectHash.PowerShell.Id

powershell Id

$ObjectHash.Winrm.DisplayName

display name

Example #9: Convert Hashtable output to the JSON / XML format.

We can convert the output of the hashtable to JSON or XML format using ConvertTo-JSON or ConvertTo-XML command.

$servicetable | ConvertTo-Json

example 9

For XML,

$servicetable | ConvertTo-Xml

example 9-1

Conclusion

Hashtables are heavily used in the PowerShell language because of their flexibility. We can easily store values, search values, and retrieves the values and the most useful thing is the output is in the table format so it can be converted to the JSON format as well. ConvertFrom-StringDate converts the output to the hashtable format.

Recommended Articles

This is a guide to PowerShell Dictionary. Here we discuss the definition, syntax, How does the dictionary work in PowerShell? and examples. You may also have a look at the following articles to learn more –

  1. PowerShell Like Operator
  2. PowerShell Match
  3. PowerShell Boolean
  4. Powershell Copy File
MICROSOFT POWER BI Course Bundle - 8 Courses in 1
34+ Hours of HD Videos
8 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
CYBER SECURITY & ETHICAL HACKING Course Bundle - 13 Courses in 1 | 3 Mock Tests
64+ Hours of HD Videos
13 Courses
3 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
MICROSOFT AZURE Course Bundle - 15 Courses in 1 | 12 Mock Tests
62+ Hour of HD Videos
15 Courses
12 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
KALI LINUX Course Bundle - 6 Courses in 1
20+ Hours of HD Videos
6 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Popular Course in this category
WINDOWS POWERSHELL Course Bundle - 7 Courses in 1
 19+ Hours of HD Videos
7 Courses
Verifiable Certificate of Completion
  Lifetime Access
4.5
Price

View Course
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more