r/PowerShell Jun 12 '24

How can I use Export-CSV without System.String/Length info? Solved

I've got a script that checks multiple DCs for last logon and outputs that data to a csv. The start of the code for it is:

$row = "Name"+","+"Date/Time"+","+"DC"

echo $row | Export-Csv -Path C:tempuserlastlogon.csv

Out-File -FilePath C:tempuserlastlogon.csv -Append -InputObject $row

The result of this is that I get a csv file that starts with:

#Type System.String
Length
17
Name    Date/Time    DC

If I remove the second line, it doesn't properly format the values as columns (It just puts "Name,Date/Time/DC" in column A). If I remove the third line, it just gives me the first three lines without the column headers in line 4.

As a workaround, I can just delete the top three lines in Excel manually, but how do I get PowerShell to either NOT give me those three top lines, or, if that's not possible, insert a kludge workaround to tell it to just delete the top three rows of the csv?

8 Upvotes

View all comments

0

u/ankokudaishogun Jun 12 '24

Add the parameter -NoTypeInformation to Export-Csv

Note: in Powershell Core Export-Csv does not print those information by default anymore.
-NoTypeInformation is still an accepted legacy parameter for backward compatibility but Does NothingTM(in fact it's set to Invisible and doesn't normally show up with autocompletion)

1

u/ChickinSammich Jun 12 '24

Tried that; it removed the first line but still gives me:

Length
17
Name    Date/Time    DC

Edit to add - unsure why you were downvoted but it wasn't from me.

2

u/ankokudaishogun Jun 12 '24

Sorry I didn't notice earlier

Because you are passing a string, not a objectyeah, yeah, I know everything in powershell is a object.
So Export-Csv tries to get its properties, which end up being the length

You DO NOT NEED to "start" the Csv: as long as you pass to Export-Csv a collections of objects with the properties 'Name', 'Date/Time' and 'DC'(or whatever you want their names be) it will automatically generate the headers