8

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

3 Answers 3

8

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.

Sign up to request clarification or add additional context in comments.

Comments

6

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

Import-Csv D:\temp\input.csv | Format-Table

2 Comments

and of course that worked haha - any idea why the Import will work sometimes and not other times?
There are several available options: Get-Command format* -CommandType cmdlet According to book PowerShell in Action: PowerShell is a type-based system, types are used to determine how things are displayed. However, normal objects don’t usually know how to display themselves. PowerShell deals with this by including a database of formatting information for different types of objects. This is part of the extended type system, which is an important component of the overall system.
0

Does the file is Unicode encoded? If so, open the csv file in notepad and save it as UTF-8, it should solve your issue.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.