Easy Parsing of Setting Files (Part 2)

by Jul 27, 2017

In the previous tip we used ConvertFrom-StringData to parse plain text key-value pairs into hash tables.

Here is an example where such conversion fails:

$settings = @'
Machine=Server12
Path=c:\test
'@

$settings | ConvertFrom-StringData

When you look at the result, you’ll quickly see why:

 
Name                           Value                                                                                        
----                           -----                                                                                        
Machine                        Server12                                                                                     
Path                           c:	est
 

Apparently, ConvertFrom-StringData treats “\” as an escape character, and in above example adds a tab (“\t”) and eats the literal “t”.

To work around this, always escape “\” with “\\”. Here is the corrected code:

$settings = @'
Machine=Server12
Path=c:\\test
'@

$settings | ConvertFrom-StringData

The result now looks just fine:

 
Name                           Value                                                                                        
----                           -----                                                                                        
Machine                        Server12                                                                                     
Path                           c:\test
 

Twitter This Tip! ReTweet this Tip!