Loading and Saving Options in JSON Format

by Dec 20, 2017

If you’d like to persist information in your script, you might want to save your data as an object in JSON format. Here is an example:

# define options object $options = [PSCustomObject]@{ Color = 'Red' Height = 12 Name = 'Weltner' } # play with options settings $options.Color = 'Blue' # save options to file $Path = "c:\test\options.json" $options | ConvertTo-Json | Set-Content -Path $Path # load options from file $options2 = Get-Content -Path $Path | ConvertTo-Json 

Just make sure the path “c:\test” exists. The script creates a custom object with the data, then saves it to disk using JSON. Next, the data can be loaded again anytime you need it, i.e. in another script, or to load initial settings.

Twitter This Tip! ReTweet this Tip!