Home > Net >  How to output Get-ADUser as csv without including the fieldnames in the first row using powershell?
How to output Get-ADUser as csv without including the fieldnames in the first row using powershell?

Time:02-11

I am trying to use the Get-ADUser to get a dump of the users in our system, and then output the contents to a CSV file.

However, I have not yet been able to figure out how to exclude the fieldnames from the first row in the csv file.

I want to have just the data [in csv format] but without the header row.

This code produces the data I want:

Get-ADUser -Filter * -SearchBase "OU=My,OU=OrganisationName,OU=Accounts,OU=Groups and Accounts,DC=myorg,DC=com" -Properties * | 
    Sort-Object -Property sn, GivenName, UserPrincipalName | 
        Select-Object name, sn, GivenName, UserPrincipalName | 
            Export-Csv -Path "C:\temp1.csv" -NoTypeInformation

But it insists on including the header row (despite the -NoTypeInformation flag).

I have tried piping the data through the command:

Format-Table  -HideTableHeaders

Like so:

Get-ADUser -Filter * -SearchBase "OU=My,OU=OrganisationName,OU=Accounts,OU=Groups and Accounts,DC=myorg,DC=com" -Properties * | 
    Sort-Object -Property sn, GivenName, UserPrincipalName | 
        Select-Object name, sn, GivenName, UserPrincipalName | 
            Format-Table -HideTableHeaders | 
                Export-Csv -Path "C:\temp1.csv" -NoTypeInformation

But this converts the data in the csv file to gibberish.

Does anyone have any thoughts on how I might get rid of the header row?

thanks heaps,

David :-)

CodePudding user response:

Instead of Export-CSV use ConvertTo-CSV skip the first line using Select-Object -Skip 1 then save the file using Out-File

instead of this:

 [...]Select-Object name, sn, GivenName, UserPrincipalName | 
            Export-Csv -Path "C:\temp1.csv" -NoTypeInformation

Do:

 [...]Select-Object name, sn, GivenName, UserPrincipalName | 
 ConvertTo-Csv -NoTypeInformation | Select -Skip 1 |
 Out-File "C:\temp1.csv"
  • Related