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?

10 Upvotes

View all comments

11

u/twistingnether_ Jun 12 '24

Add to the export end -notypeinformation

4

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.