Powershell export-csv with no headers?
Asked Answered
M

2

24

So I'm trying to export a list of resources without the headers. Basically I need to omit line 1, "Name".

Here is my current code:

Get-Mailbox -RecipientTypeDetails RoomMailbox,EquipmentMailbox | Select-Object Name | Export-Csv -Path "$(get-date -f MM-dd-yyyy)_Resources.csv" -NoTypeInformation

I've looked at several examples and things to try, but haven't quite gotten anything to work that still only lists the resource names.

Any suggestions?

Myrtice answered 15/10, 2014 at 18:51 Comment(0)
T
39

It sounds like you basically want just text a file list of the names:

Get-Mailbox -RecipientTypeDetails RoomMailbox,EquipmentMailbox |
 Select-Object -ExpandProperty Name | 
 Set-Content -Path "$(get-date -f MM-dd-yyyy)_Resources.txt"

Edit: if you really want an export-csv without a header row:

(Get-Mailbox -RecipientTypeDetails RoomMailbox,EquipmentMailbox |
Select-Object Name |
ConvertTo-Csv -NoTypeInformation) |
Select-Object -Skip 1 |
Set-Content -Path "$(get-date -f MM-dd-yyyy)_Resources.csv"
Tripterous answered 15/10, 2014 at 19:2 Comment(5)
Well it does need to be a .csv for sure, as we are importing information into databases. We will doing this with many different things, but this resource one is the easiest/least amount of code. Will this work with a csv as well? I can test in a while. Thanks!Myrtice
If you get rid of that first line, then it doesn't have a header row any more, so technically it's not really a CSV file. Do the names need to be quoted?Tripterous
I see what you mean... What has been asked of me, is to get rid of that first line (header) so that they can code Access to pull everything from whatever columns they need. In this case, just column A. Does that make sense?Myrtice
Okay, I posted an updated answer to include a script method for that.Tripterous
Perfect also for APPENDING to an existing CSV file, much appreciate. I haven't seen that case mentioned, so I'll give it representation. I'm using Add-Content rather than Set, but Out-File -Append should also work.Foreword
D
3

Powershell 7 is out now. Still no way to export-csv without headers. I get it. Technically it wouldn't be a CSV without a header row.

But I need to remove the header row, so

$obj | convertto-csv | select-object -skip 1 |out-file 'output.csv'

P.S. I didn't need the quotes and I wanted to filter out rows based on a certain property value:

$obj | where-object {$_.<whatever property> -eq 'X' } | convertto-csv -usequotes never | select-object -skip 1 |out-file 'output.csv'
Distil answered 16/4, 2021 at 18:27 Comment(1)
The other (accepted) answer showed how to skip a row for 7 years before you posted this. What does this answer add (which answers the question) which was missing from the existing answer?Yah

© 2022 - 2024 — McMap. All rights reserved.