Using the ConvertFrom-StringData Cmdlet in PowerShell

A few months ago, I wrote about the amazing Hashtable Module, but PowerShell also has a built-in option for converting key-value pairs to a hash table. In this blog post, I will show you how it works.

What does ConvertFrom-StringData do?

“The ConvertFrom-StringData cmdlet converts a string that contains one or more key and value pairs into a hash table. Because each key-value pair must be on a separate line, here-strings are often used as the input format. By default, the key must be separated from the value by an equals sign (=) character.

The ConvertFrom-StringData cmdlet is considered to be a safe cmdlet that can be used in the data section of a script or function. When used in a data section, the contents of the string must conform to the rules for a data section. For more information, see about_Data_Sections.

ConvertFrom-StringData supports escape character sequences that are allowed by conventional machine translation tools. That is, the cmdlet can interpret backslashes (\) as escape characters in the string data by using the Regex.Unescape Method, instead of the PowerShell backtick character (`) that would normally signal the end of a line in a script. Inside the here-string, the backtick character doesn’t work. You can also preserve a literal backslash in your results by escaping it with a preceding backslash, like this: \\. Unescaped backslash characters, such as those that are commonly used in file paths, can render as illegal escape sequences in your results.

PowerShell 7 adds the Delimiter parameter.”

Source: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/convertfrom-stringdata?view=powershell-7.6

Use cases

It’s a simple way to convert strings with delimiters into a Hashtable. It can also be used to read the contents of a file using Get-Content together with the -Raw Parameter (which reads the file as a single multi-line string instead of an array of separate strings; this avoids creating an array of hashtables and returns a single hashtable). File types could be simple config or .ini files, but also .env, .properties, etc., as well as log files or email bodies.

Other use cases include converting CLI tool output, building quick lookup tables or mapping dictionaries (and avoiding a large Switch statement, for example).

Things to avoid or don’t work

It doesn’t work, however, with nested or multi-level data (Use ConvertFrom-Json or ConvertFrom-Yaml from the powershell-yaml module instead). It also doesn’t work with duplicate keys in the same block because of the Hashtable conversion.

Backslashes are escape characters. ConvertFrom-StringData Processes escape sequences via regex unescaping, meaning file paths like C:\Temp will fail unless doubled (C:\\Temp) or replaced beforehand using .Replace('\', '\\')

Comments are ignored by default; lines starting with # are ignored, but inline comments on the same line as the key-value pair are not supported.

How to use

The Cmdlet is inside Microsoft.PowerShell.Utility Module and is available by default in both PowerShell v5 and v7. But… The Delimiter value, as mentioned in the first chapter, is only available in v7.

Parameters

Delimiter

The default Delimiter is the equals (=) character, but you can choose your own depending on the situation (e.g., “,”, “;”, space, etc.).

StringData

You can use this to specify the string to be converted. Alternatively, you can pipe contents to ConvertFrom-StringData. It must be a string that contains one or more key-value pairs, which should all be on separate lines or separated by the newline character (`n)

Examples

Reading from a file

In the example below, I used Get-Content -Raw to read all the lines from a sample config.txt file that contained path names. As mentioned above, you first need to replace single Backslashes with double backslashes; I used “.replace(‘\’,’\\’)” for that around the Get-Content Cmdlet. And ConvertFrom-StringData will show that, like below, with the default = Delimiter separating the key-value pairs:

Nice key-value files that you can store in a Variable, for example:

The contents of the config.txt file were:

Converting CLI Output

In the example below, I ran sw_vers (which shows information about your macOS device, including product and build version) and used ConvertTo-StringData to store it in a variable called $Info.

Wrapping up

And that’s how you use the built-in ConvertFrom-StringData to easily store contents or output in a Hashtable for further use. Have a lovely weekend!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.