PowerShell supports a wide variety of text file formats, so what’s the best way to save and read data?
This largely depends on the type of data, so here is the second part of a practical guideline:
- Three-Dimensional: when you need to persist data that has columns and rows and can contain nested objects, choose the widely accepted JSON file format. The cmdlets of choice use the “Json” noun: ConvertTo-Json, ConvertFrom-Json. There are no cmdlets to directly read and write the JSON string information to file but you can simply combine them with Get/Set-Content to emulate the behavior of the missing Import/Export-Json. JSON stores arrays of objects that can have fully object-oriented nested properties (full object tree). PowerShell 7 adds a new Test-Json cmdlet. Caveat: when reading and writing to file, you need to specify the same encoding. While PowerShell 7 for most cmdlets defaults to UTF8, this is not the case for Windows PowerShell, so you should always explicitly define the text encoding when you read/write text files. Caveat #2: there are different JSON specifications. and the built-in JSON cmdlets may or may not suffice for your task. That’s why vendors and community have added many own flavors of JSON cmdlets, i.e. VMWare ships ConvertFrom-JsonX whereas MicrosoftTeams added their ConvertTo-JsonForPSWS.
- Four-Dimensional: when you need high fidelity and want to store and retrieve full objects, including their nested structure and including their original data type, then XML is the way to go. The cmdlets of choice use the “CliXml” noun: Import/Export-CliXml. They are designed to persist your own data to file and later retrieve the saved data in the most similar form. Caveat: As always with text files, when reading and writing to file, you need to specify the same encoding. While PowerShell 7 for most cmdlets defaults to UTF8, this is not the case for Windows PowerShell, so you should always explicitly define the text encoding when you read/write text files. Caveat #2: Both cmdlets use their own internal XML structure (“Cli” = “Command Line Interface”) to describe objects. Import-CliXml can only load XML from files that have originally been created by Export-CliXml. You cannot use them to work with XML files from other sources.