Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying csv in powershell

Tags:

powershell

csv

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
like image 620
User_1403834 Avatar asked Dec 20 '12 19:12

User_1403834


People also ask

How do I get the output of a PowerShell script from CSV?

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.

How does Import CSV work in PowerShell?

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.


2 Answers

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.

like image 185
Paul Rowland Avatar answered Oct 10 '22 22:10

Paul Rowland


You could redirect output of Import-Csv to Format-Table:

Import-Csv D:\temp\input.csv | Format-Table
like image 44
Akim Avatar answered Oct 10 '22 22:10

Akim