CmdID / PowerShell / Cmdlets / ConvertFrom
ConvertFrom-StringData
Converts a string containing one or more key and value pairs to a hash table. PowerShell reference
From the PowerShell reference 1 parameter
What it does
Converts a string containing one or more key and value pairs to a hash table. 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.
ConvertFrom-StringData [-StringData] <String> [<CommonParameters>]
Convert a single-quoted here-string to a hash table
$Here = @'
Msg1 = The string parameter is required.
Msg2 = Credentials are required for this command.
Msg3 = The specified variable doesn't exist.
'@
ConvertFrom-StringData -StringData $HereIdentifyConvert a here-string containing a comment
ConvertFrom-StringData -StringData @'
Name = Disks.ps1
# Category is optional.
Category = Storage
Cost = Free
'@IdentifyConvert a string to a hash table
$A = ConvertFrom-StringData -StringData "Top = Red n Bottom = Blue"
$AIdentifyUse in the data section of a script
$TextMsgs = data {
ConvertFrom-StringData @'
Text001 = The $Notebook variable contains the name of the user's system notebook.
Text002 = The $MyNotebook variable contains the name of the user's private notebook.
'@
}
$TextMsgsIdentifyUse the pipeline operator to pass a string
$Here = @'
Msg1 = The string parameter is required.
Msg2 = Credentials are required for this command.
Msg3 = The specified variable doesn't exist.
'@
$Hash = $Here | ConvertFrom-StringData
$HashIdentifyUse escape characters to add new lines and return characters
ConvertFrom-StringData @"
Vincentio = Heaven doth with us as we with torches do,\nNot light them for themselves; for if our virtues\nDid not go forth of us, 'twere all alike\nAs if we had them not.
Angelo = Let there be some more test made of my metal,\nBefore so noble and so great a figure\nBe stamp'd upon it.
"@ | Format-ListIdentifyUse backslash escape character to correctly render a file path
ConvertFrom-StringData "Message=Look in C:\\Windows\\System32"IdentifyIts page in the reference: ConvertFrom-StringData.
Parameters
-StringDataSpecifies the string to be converted. You can use this parameter or pipe a string to ConvertFrom-StringData. The parameter name is optional.
The value of this parameter must be a string that contains one or more key-value pairs. Each
key-value pair must be on a separate line, or each pair must be separated by newline characters
( n ).
You can include comments in the string, but the comments can't be on the same line as a key-value pair. ConvertFrom-StringData ignores single-line comments. The hash character (#) must be the first non-whitespace character on the line. All characters on the line after the hash character (#) are ignored. The comments aren't included in the hash table.
A here-string is a string consisting of one or more lines. Quotation marks within the here-string are interpreted literally as part of the string data. For more information, see about_Quoting_Rules.