. Advertisement .
..3..
. Advertisement .
..4..
If you are among those who do not know how to import and export CSV in PowerShell, tune in to our guidelines. We will provide concise and straight-to-the-point instructions to help solve the dilemma.
How to Import and Export CSV in PowerShell?
1. Export Data
To export your data to CSV files in Powershell, you may turn to Out-File – a common cmdlet that helps programmers store outputs to the files. The command illustrated below will get the top nine CPU-operating processes in a CSV file.
Get-Process | Sort-Object CPU -Descending | Select -First 09 |
Out-File C:\Temp\Top09Procs.csv
Try to run the codes, and you will realize that the output could be retrieved by the cmdlet, though it’s in single columns, reducing user readability and not really a great way to tackle the data.
Hence, PowerShell offers some other inbuilt cmdlets called Export-CSV, dumping data into CSV files. Still, it’s a must to select command properties this time, since certain commands will expose every property once your output has successfully been exported to CSV files.
Get-Process | Sort-Object CPU -Descending | Select -First 09 | `
Select Name, id, CPU, WorkingSet | Export-Csv C:\Temp\Top09Procs.csv
The output sometimes will show Information Type. Don’t need it? Then you may add the parameter “NoTypeInformation” at the endpoint of your Export-CSV cmdlets.
Suppose you work on one script that calls for data appending into CSV files. In such cases, we suggest you use the parameter “-Append”. For instance, in the example here, we used the same command above for the loop “foreach” to append our data.
$procs = Get-Process | Sort-Object CPU -Descending | Select -First 09 | `
Select Name, id, CPU, WorkingSet
foreach($proc in $procs){
$proc | Export-Csv C:\Temp\Top09Procs.csv -NoTypeInformation -Append
}
It’s also possible to export CSV files from PSCustomObject or Hashtable. For example:
[PSCustomObject]@{
Name = 'OneDrive'
ID = '1000'
Memory_Usage = '10%'
CPU_Usage = '09%'
} | Export-Csv C:\Temp\SingleProcessInfo.csv -NoTypeInformation
2. Import Data
Data import from CSV files is as straightforward as export. For data importation to CSV files, you must use the inbuilt cmdlet “Import-CSV”.
Again, let’s come back to our previous illustration of the files established by our top 9 high utilization CPU processes. This command below will proceed to import those CSV files.
Import-Csv C:\Temp\Top09Procs.csv
You may also perform normal operations here, which run on the pipeline.
Import-Csv C:\Temp\Top09Procs.csv | Select -First 2
To choose specific properties, run the commands:
Import-Csv C:\Temp\Top09Procs.csv | Select Name, ID
It’s also possible to sort CSV output data via a pipeline.
Import-Csv C:\Temp\Top09Procs.csv | Sort-Object -Property Name
And filter specific data with:
Import-Csv C:\Temp\Top09Procs.csv | where{$_.Name -eq "OneDrive"}
Conclusion
This article has shed light on tips to import and export CSV in PowerShell. Hopefully they will be of some help! For other PowerShell issues (such as adding array items), feel free to turn to our ITtutoria website.
Leave a comment