Command-separated Value or
CSV files are a very popular type of text files which are used for different purposes. What makes CSV special is the ability to store structured data by delimiting the values or columns with comma. CSV files are compatible with Excel files to import and export.
Export To Comma Delimited CSV File
We will start with a simple example.
Get-Process command is used to list currently running proccesses with different information like Handle, NPM, CPU,ID, ProcessName etc. We can use the command Export-CSV in order to store these process information delimited with command.
Now we can put these running processes information into a CSV file by redirecting Get-Process output. We will provide the file name with the option
-Path. The file name will be ProcessList.csv.
PS> Get-Process | Export-CSV -Path ProcessList.csv
Alternatively we can provide different directory with the option -Path. In the following command we will put the ProcessList.csv file into the
PS> Get-Content 'C:\Users\İsmail Baydan\Desktop\ProcessList.csv'
Set Delimiter For CSV File
Delimiters are an important part of the CSV files. By default, CSV files use the command as a delimiter. But we can change delimiter whatever we want. We will use the option -Delimeter in order to set a different delimiter character. In the following example, we will set the equal sign = as the delimiter. Putting the delimiter inside the double quotes is a very good way to prevent errors.
PS> Get-Process | Export-CSV -Delimiter "=" -Path ProcessList.csv
Export To Semicolon Delimited CSV File
The comma is the standard delimiter for the CSV files. But we can use different delimiters in order to separate columns. In some cases, a comma can be used inside to content we want to put into the CSV file. This will crash the CSV file because delimiter and content will be same and it will be error-prone to parse the content with the delimiter. So good alternative to the command delimiter is semicolon which is rarely used in CSV file contents. In the following example, we will set semicolon as delimiter.
PS> Get-Process | Export-CSV -Delimiter ";" -Path ProcessList.csv
Select Properties To Write CSV File
Up to now we have worked with the Get-Process command which will list all properties of the currently running processes. In some cases we do not need to put all properties into the CSV file. We can select which properties can be put into the CSV file. We will use the command
Select-Object and its option
-Property like below.
PS> Get-Process | Select-Object -Property ProcessName,Id | Export-CSV -Path ProcessList.csv
Append To The Existing CSV File
Another use case for the command Export-CSV is adding new CSV content into the existing CSV content. We can use the option
-Append which will add new CSV content into the existing file.
PS> Get-Process | Export-CSV -Delimiter -Append -Path ProcessList.csv
Do Not Overwrite Existing CSV File
By default command Export-CSV will overwrite on the existing file without any warning. We can prevent this actions with the option
-NoClobber which will do not over-write existing file and print and error realted given file allready exists.
PS> Get-Process | Export-CSV -NoClobber -Path ProcessList.csv
Overwrite Read-Only Files
By default read-only files can be edited, appended by default. But Export-CSV command can change the read-only attribute for editing and then revert back to read-only. We will force to write operation with the option
-Force like below.
PS> Get-Process | Export-CSV -Append -Force -Path ProcessList.csv
Set CSV File Encoding
As CSV file is a text file it has an encoding option. By default, the UTF-8 is the default encoding for the text and CSV files. But in some cases, we may need to change the default encoding and set specific encoding. We can use the option -Encoding and provide an encoding name like ASCII. In the following example, we will set the CSV file encoding as ASCII.
PS> Get-Process | Export-CSV -Encoding ASCII -Path ProcessList.csv
Below are popular and available encoding formats which can be used with the Export-CSV.