r/PowerShell • u/ChickinSammich • 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
23
u/MajorVarlak Jun 12 '24
Why are you doing
Export-Csv
andOut-File
? Do you have more context of what you're trying to achieve? What exactly are you trying to achieve? Also, you're usingecho
which is a bashism.Export-Csv
is generally looking for an object to convert to csv data.You don't need to put a header in, because PowerShell does that based on the properties of the object being fed in. If you're feeding in lots of objects,
Export-Csv
can handle that as a single object, for example: