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 first part of a practical guideline:
- One-Dimensional: when data has just one dimension, i.e., lists with server names, it is best to save this as a plain text file. The cmdlets of choice use the “Content” noun: Get-Content reads, Set-Content writes, Add-Content appends. Plain text files store string arrays. Caveat: when saving and reading you need to use the same text encoding, so best use UTF8. Caveat #2: Add -ReadCount 0 parameter if you want to read the string array file content to a variable. This is multiple times faster than by default.
- Two-Dimensional: data that comes in columns and rows is best saved as CSV. The cmdlets of choice use the “Csv” noun: Export-Csv, Import-Csv. CSV-files store arrays of objects. The CSV column names define the object properties. Caveat: since CSV is based on text, make sure you actively choose a good text encoding such as UTF8. Caveat #2: CSV files can use many different delimiters so either use -Delimiter to make sure *you* define the delimiter at all times, or use -UseCulture if you want to make sure the automatically selected delimiter matches other applications *on the same system*, i.e., when you plan to open the CSV file later with Microsoft Excel.