Scripting. Powershell, VMware, Windows, Active Directory & Exchange. All that kind of stuff…..
RSS icon Email icon Home icon
  • PowerShell 2.0: One Cmdlet at a Time #85 ConvertFrom-StringData

    Posted on March 29th, 2010 Jonathan Medd No comments

    Continuing the series looking at new cmdlets available in PowerShell 2.0. This time we look at the ConvertFrom-StringData cmdlet.

    What can I do with it?

    Converts a string which contains one or multiple key and valuepairs into a hash table. Input is typically from a here-string since each key and value must be on a separate line.

    Example:

    Create a here-string and store it in the variable $herestring. Convert it into a hash table.

    $herestring = @'
    Fruit1 = Orange
    Fruit2 = Apple
    '@
    
    $herestring | ConvertFrom-StringData

    You will notice that the data is now in the form of a hash table

    ConvertFrom-HereString

    How could I have done this in PowerShell 1.0?

    To create a new hash table you could use a new .Net Object of type System.Collections.Hashtable, something like the below

    $strings = ('Fruit1 = Orange','Fruit2 = Apple')
    $table = New-Object System.Collections.Hashtable
    
    foreach ($string in $strings){
    
    $split = $string.split("=")
    $part1 = $split[0]
    $part2 = $split[1]
    $table.add("$part1","$part2")
    }

    1000 things 1% better!

    Leave a reply