Trending September 2023 # How Does The Dictionary Work In Powershell? # Suggested October 2023 # Top 14 Popular | Phuhoabeautyspa.com

Trending September 2023 # How Does The Dictionary Work In Powershell? # Suggested October 2023 # Top 14 Popular

You are reading the article How Does The Dictionary Work In Powershell? updated in September 2023 on the website Phuhoabeautyspa.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 How Does The Dictionary Work In Powershell?

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:

The syntax for Ordered Dictionary:

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

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?

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:

$htable

Output:

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:

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

$dict = [ordered]$htable

Output:

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:

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

$hash = [hashtable]$htable

Output:

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.

Output:

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

Output:

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

$servicetable

Output:

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:

To retrieve hashtable values,

servicetable.Values

Output:

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

$servicetable.Name

Output:

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

Output:

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

$servicetable

Output:

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

Output:

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

Output:

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

Output:

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

Output:

We need to add values now.

$servicetable

Output:

Name property will give multiple values now.

Example #7: Converting string data to the hashtable.

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

“@

Output:

To convert to the hashtable,

Output:

Example #8: Hashtable with Object Types.

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

}

Output:

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

$ObjectHash.PowerShell

$ObjectHash.winrm

Further expanding,

$ObjectHash.PowerShell.Id

$ObjectHash.Winrm.DisplayName

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.

For XML,

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 –

You're reading How Does The Dictionary Work In Powershell?

Update the detailed information about How Does The Dictionary Work In Powershell? on the Phuhoabeautyspa.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!