How to Convert String to Hashtable in PowerShell?

Recently, I got a requirement to convert string to hashtable in PowerShell. I tried different methods. In this tutorial, I will show you how to convert string to hashtable in PowerShell.

To convert a string to a hashtable in PowerShell, you can use the ConvertFrom-StringData cmdlet. This cmdlet converts a string containing key-value pairs, with each pair on a separate line, into a hashtable. For example, the string “key1=value1nkey2=value2” can be converted using ConvertFrom-StringData “key1=value1nkey2=value2” to produce a hashtable with keys “key1” and “key2”.

Convert String to Hashtable in PowerShell

There are different methods to convert string to hashtable in PowerShell. Let me show you examples.

Method 1: Using ConvertFrom-StringData

The ConvertFrom-StringData cmdlet is the best way to convert a string containing key-value pairs into a hashtable in PowerShell. Each key-value pair must be on its own line, separated by an equals sign (=).

Here is an example of how to convert a string to a hashtable in PowerShell using ConvertFrom-StringData.

# Define a string with key-value pairs
$stringData = @"
Name=John Doe
Age=30
City=New York
"@

# Convert the string to a hashtable
$hashtable = ConvertFrom-StringData $stringData

# Display the hashtable
$hashtable

In this example, the $stringData variable contains a multi-line string with key-value pairs. The ConvertFrom-StringData cmdlet converts this string into a hashtable. You can then access the values using their keys, such as $hashtable['Name'].

I executed the above PowerShell script using VS code, and you can see the output in the screenshot below:

Convert String to Hashtable in PowerShell

Check out How to Convert String to Object in PowerShell?

Method 2: Using ConvertFrom-Json

If your string is in JSON format, you can use the ConvertFrom-Json cmdlet to convert it into a hashtable. JSON is a common format for data interchange, and PowerShell provides native support for it.

Here is an example of how to use ConvertFrom-Json to convert string to hashtable in PowerShell.

# Define a JSON string
$jsonString = '{"Name": "John Doe", "Age": 30, "City": "New York"}'

# Convert the JSON string to a hashtable
$hashtable = $jsonString | ConvertFrom-Json

# Display the hashtable
$hashtable

In this example, the $jsonString variable contains a JSON string with key-value pairs. The ConvertFrom-Json cmdlet converts this string into a hashtable.

You can see the output in the screenshot below after I executed the above PowerShell script.

PowerShell Convert String to Hashtable

Read How to Write JSON to File in PowerShell?

Method 3: Using Custom Parsing

For more complex or custom string formats, you might need to parse the string and build the hashtable manually. This method provides flexibility but requires more code.

Here is an example.

# Define a custom string with key-value pairs
$customString = "Name:John Doe;Age:30;City:New York"

# Split the string into individual key-value pairs
$pairs = $customString -split ';'

# Initialize an empty hashtable
$hashtable = @{}

# Iterate over each pair and add it to the hashtable
foreach ($pair in $pairs) {
    $key, $value = $pair -split ':'
    $hashtable[$key] = $value
}

# Display the hashtable
$hashtable

In this example, the $customString variable contains a custom-formatted string. The string is split into individual key-value pairs using the -split operator, and then each pair is added to the hashtable.

Check out the output in the screenshot below:

How to Convert String to Hashtable in PowerShell

Read Convert String to Int in PowerShell

Method 4: Using Splatting for Command Parameters

Splatting is a feature in PowerShell that allows you to pass parameters to a command as a hashtable. This method can be useful when you need to convert a string to a hashtable for use as command parameters.

Here is an example.

# Define a string with command parameters
$paramString = "Name=John Doe;Age=30;City=New York"

# Split the string into individual key-value pairs
$pairs = $paramString -split ';'

# Initialize an empty hashtable
$hashtable = @{}

# Iterate over each pair and add it to the hashtable
foreach ($pair in $pairs) {
    $key, $value = $pair -split '='
    $hashtable[$key] = $value
}

# Use the hashtable for splatting
Get-UserDetails @hashtable

In this example, the $paramString variable contains a string with command parameters. The string is split into individual key-value pairs, which are then added to the hashtable. The hashtable is used for splatting to pass parameters to the Get-UserDetails command.

Conclusion

In this tutorial, I have explained how to convert a string to a hashtable in PowerShell using various methods, such as ConvertFrom-StringData, ConvertFrom-Json, Custom Parsing, and Splatting for Command Parameters.

I hope this helps.

You may also like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.