Loading a PowerShell hashtable from a file?

前端 未结 6 1155
粉色の甜心
粉色の甜心 2020-12-11 14:45

I\'ve got a file containing some data in PowerShell Object Notation:

@{ X = \'x\'; Y = \'y\' }

I\'d like to load this into a variable from

6条回答
  •  天涯浪人
    2020-12-11 15:24

    I've used ConvertFrom-StringData. If you want to use this approach you'll need to change the way you store key/value pairs with each on its own line and no quotes:

    #Contents of test.txt
    X = x
    Y = y
    
    get-content .\test.txt | ConvertFrom-StringData
    
    Name                           Value
    ----                           -----
    X                              x
    Y                              y
    

    ConvertFrom-StringData is a built-in cmdlet. I created corresponding ConvertTo-StringData function available here http://poshcode.org/1986

提交回复
热议问题