Partager via


Powershell 1 liner: Key Value pairs from text file to hashtable

Example Problem:
You have a text file that contains key value pair and you want to use them as a hash table in powershell.

Values.txt:
key1=value1
key2=value2
key3=value42

One way to do it:
PS C:\temp> gc values.txt | %{$h = @{}} {if ($_ -match "(.*)=(.*)") {$h[$matches[1]]=$matches[2];}}

PS C:\temp> $h

 Name                           Value
----                           -----
key3                           value42
key2                           value2
key1                           value1