The question Loading a PowerShell hashtable from a file? documents how to load a file that contains a hashtable in PSON format into a variable, but how does one save a hashtable to a file in PSON format?
Hashtable:
@{
"name" = "report 0"
"parameters" = @(
@{"name" = "parameter 0"; "default" = 1; "values"=1,2,3,4},
@{"name" = "parameter 1"; "default" = 'A'; "values" = 'A','B','C'}
)
}
Try the
*-CliXml
cmdlets. To save the object:To read it back:
One way would be to put the hashtable definition in a scriptblock:
@{
"name" = "report 0"
"parameters" = @( @{"name" = "parameter 0"; "default" = 1; "values"=1,2,3,4}, @{"name" = "parameter 1"; "default" = 'A'; "values" = 'A','B','C'} )
}
Within the script, you'd need to invoke the script block to instantiate the hashtable:
How to use a shorthand "object notation" to generate an object in PowerShell:
DISCLAIMER: I know this does not answer OP's question directly but it might help folks like me searching for a very similar issue and landing here.
I came to the same question.
To use the ConvertTo-JSON is indeed the most obvious work around but in earlier versions of PowerShell this Cmdlet is not available, besides why should you speak another language (e.g. JSON) if you do not want to interchange any data with another language? I found out that a ConvertTo-PSON Cmdlet will be nice to store data and also be very helpful in displaying, revealing and exploring the exact information of an object variable, so this is the result:
I have nicknamed (aliased) it to simply PSON as a ConvertFrom-PSON cmdlet indeed already exist in the form of: Invoke-Expression:
It converts most (all?) of the native PowerShell object types which I have listed here in a single test object:
To convert the object to PSON string you can simply give the command:
This basic example will converts the object into a PowerShell string similar to the ConvertTo-JSON cmdlet which can be converted back again with the native Invoke-Expression PowerShell cmdlet.
Strict
The ConvertTo-PSON has also a –Strict option which prevents any type casting by explicitly adding the type names this will give a big advantage to the JSON format. This type casting will usually happen with the object type when to convert them:
To store the object in e.g. a file, registry, database, etc. the best is to use the –Strict option and define 0 layers which will compress the data (remove all spaces):
Layers
The layers option will define how the layer levels will expanded over separate lines. Each layer level will be get an extra tab indent.
-Layers 0 One line without any spaces (compressed mode)
-Layers 1 One line formatted with spaces (default)
-Layers X Multiple layers are expanded over multiple lines until a depth of X levels (Indents)
Let’s convert the PSON string back and confirm the results:
This is the result:
Depth
The depth option is similar to the depth option in then ConvertTo-JSON command. By default the depth is limited to 9 levels. A depth of 0 levels will remove the double quotes from strings this might come in handy where you expect a string but would like to be alerted when it is not, e.g.:
Version
PowerShell 2.0 does not except the
[PSCustomObject]
type which will be replace withNew-Object PSObject –Property
when ran PowerShell 2.0, if you like to interchange data from a higher version of PowerShell with PowerShell 2.0 you can use the –Version 2.0 option.Examples
Using the hashtable in the original question from Craig:
Write-Host (PSON $Craig) # Standard
Write-Host (PSON $Craig -Layers 0) # Compressed
Write-Host (PSON $Craig -Layers 3) # 3 Layers
Write-Host (PSON $Craig -Strict -Layers 9) # Strict, 9 (all) layers
For the latest see:
ConvertTo-Expression
at the PowerShell Gallery.Found a solution to read/write hash tables to an INI file: