I have a CSV that I created and I'm trying to run an Import-CSV command on it. From examples I've seen, the data is displayed in a nice table format. However, I can not get my data to look that way. If I open it in Excel, everything is is it's own cell block. Below is some of my data, how it is displayed, and what I am expecting:
Data:
Directory,Name,Length,CreationTime,LastWriteTime
\\foo\foofoo\nightly.188\share\name,name.pst,271360,6/4/2009 2:42:21 PM,6/9/2011 8:58:50 AM
\\foo\foofoo\nightly.188\share\name,name2.pst,71123968,10/5/2010 2:41:56 PM,8/1/2011 4:08:32 PM
Data output:
Directory : \\foo\foofoo\nightly.188\share\name
Name : name.pst
Length : 271360
CreationTime : 6/4/2009 2:42:21 PM
LastWriteTime : 6/9/2011 8:58:50 AM
Directory : \\foo\foofoo\nightly.188\share\name
Name : name2.pst
Length : 71123968
CreationTime : 10/5/2010 2:41:56 PM
LastWriteTime : 8/1/2011 4:08:32 PM
What I'm expecting:
Directory Name Length CreationTime LastWriteTime
\\foo\foofoo\nightly.188\share\name name.pst 271360 6/4/2009 2:42:21 PM 8/1/2011 4:08:32 PM
\\foo\foofoo\nightly.188\share\name name2.pst 71123968 10/5/2010 2:41:56 PM 8/1/2011 4:08:32 PM
Getting output in a CSV file using PowerShell To get the output of any command in a CSV file, the Export-CSV cmdlet is used. It saves the output as comma-separated values. Here, the Export-CSV command will fetch the output of the Data_object and save it as a CSV file at the specified Path.
The Import-Csv cmdlet creates table-like custom objects from the items in CSV files. Each column in the CSV file becomes a property of the custom object and the items in rows become the property values. Import-Csv works on any CSV file, including files that are generated by the Export-Csv cmdlet.
The number of columns in your input.csv controls whether the default output is Format-List or Format-Table
Import-Csv D:\temp\input.csv | Format-List
Import-Csv D:\temp\input.csv | Format-Table
Import-Csv D:\temp\input.csv
This link suggests Because the output contains more than 5 properties the default layout is courtesy of Format-List
although with Import-Csv it appears to be happening when you have more than 4 columns.
I played around with window width and data in the file and it appeared 4 columns was the magic number.
You could redirect output of Import-Csv
to Format-Table
:
Import-Csv D:\temp\input.csv | Format-Table
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With