Easy Parsing of Setting Files (Part 3)

by Jul 28, 2017

In the previous tip you have discovered how ConvertFrom-StringData can turn plain text key-value pairs into a hash table. What’s missing is the other way: turn a hasht able into plain text. With that in place, you’d have a really simple framework to save settings and information to files.

Let’s first create a hash table with some data:

$test = @{
    Name = 'Tobias'
    ID = 12
    Conf = 'PowerShell Conference EU'
}

$test

The result looks like this:

 
Name                           Value                                                                                        
----                           -----                                                                                        
Conf                           PowerShell Conference EU                                                                     
Name                           Tobias                                                                                       
ID                             12
 

Now here is the function ConvertFrom-Hashtable that takes a hash table and converts it to plain text:

filter ConvertFrom-Hashtable
{
    $_.GetEnumerator() |
      ForEach-Object {
        # get hash table key and value
        $value = $_.Value
        $name = $_.Name
        
        # escape "\" in strings
        if ($value -is [string]) { $value = $value.Replace('\','\\') }
        
        # compose key-value pair as plain text
        '{0}={1}' -f $Name, $value
      }
}

Let’s see how the hash table converts:

 

PS> $test | ConvertFrom-Hashtable
Conf=PowerShell Conference EU
Name=Tobias
ID=12

PS>  
 

You can use ConvertFrom-StringData to go the other way:

 
PS> $test | ConvertFrom-Hashtable | ConvertFrom-StringData

Name                           Value                                                                                        
----                           -----                                                                                        
Conf                           PowerShell Conference EU                                                                     
Name                           Tobias                                                                                       
ID                             12                                                                                           



PS>  
 

So essentially, you can now take your hash table, save it as plain text, and reuse it later:

$test = @{
    Name = 'Tobias'
    ID = 12
    Conf = 'PowerShell Conference EU'
}

$path = "$env:temp\settings.txt"

# save hash table as file
$test | ConvertFrom-Hashtable | Set-Content -Path $path -Encoding UTF8
notepad $path 

# read hash table from file
Get-Content -Path $path -Encoding UTF8 |  
  ConvertFrom-StringData |
  Out-GridView

Note that this approach works well for simple string and numeric data. It will not work for complex data types since the conversion does not serialize objects.

Twitter This Tip! ReTweet this Tip!