EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA

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

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

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 Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW