locked
Cannot load hashtable from file. RRS feed

  • Question

  • Hi,

    I have a ps1 to export the hashtable as a file.

    Name                           Value
    ----                           -----
    G                              Green    
    R                              Red             
    B                              Blue

    However, powershell's ConvertFrom-StringData cannot read back from file.  it must read as format like:

    G=Green

    R=Red

    B=Blue

    How can I read the content from file which exported from hashtable?

    Friday, November 22, 2013 8:22 AM

Answers

  • To save an object to a file you can use the Clixml cmdlets:

    $ht=@{G="Green";R="Red";B="Blue"}
    $ht | Export-Clixml $env:TEMP\test.xml
    $ht=Import-Clixml $env:TEMP\test.xml
    

    In case you really want to read it back the way you exported it:

    $ht=@{}
    Get-Content c:\test.txt | select -Skip 2 | foreach {
    	$key,$value=$_.Split("",[StringSplitOptions]::RemoveEmptyEntries)
    	$ht.Add($key,$value)
    }

    • Edited by Dirk_74 Friday, November 22, 2013 9:09 AM
    • Proposed as answer by Wizend Friday, November 22, 2013 1:48 PM
    • Marked as answer by AnnaWY Sunday, December 1, 2013 2:38 AM
    Friday, November 22, 2013 9:03 AM