Export Information to CSV?

Hello i am trying to export my AD users to a CSV of a particular format so that i can upload them to a third party application. So far i can get the users and format them in a table with the format i want using…

  Get-ADUser -Filter * -SearchBase "OU=Current Staff,OU=Our Organisation,DC=OurOrg,DC=COM" | Format-Table Name,GivenName,Surname,UserPrincipalName -a -HideTableHeaders   

Great i have all the columns and data i need. I just want to export this to a csv that already has the headers ‘Username’ ‘First Name’ Last Name’ ‘Email’. When i pipe the above to…

  Export-Csv -Path "C:\Holding\Okta AD Exports\third-party-template.csv" 

I get a load of guff in the CSV file and the headers have gone (see atatched). What am i doing wrong? Is there a way to add custom headers when i format the table so i can just export them to a new CSV? I think its because it cant match up the data with the headers but not sure why.

Thanks in advance!


Is this what you wanted:

Get-ADUser -Filter * -SearchBase "OU=Current Staff,OU=Our Organisation,DC=OurOrg,DC=COM" | Select-Object Name,GivenName,Surname,UserPrincipalName | Export-Csv -Path "C:\Holding\Okta AD Exports\third-party-template.csv"

remove format table its not needed

Get-ADUser -Filter * -SearchBase "OU=Current Staff,OU=Our Organisation,DC=OurOrg,DC=COM" |  Export-Csv -Path "C:\Holding\Okta AD Exports\third-party-template.csv" -NoTypeInformation

The only thing you can pipe after format-table is out-file, other than that its the end of the road

As a note, consider reading “The Big Book of PowerShell Gotchas.” It’s free, very short, and it explains why Format-Table was messing you up.

Thanks for your replies everyone, sorry for the delay in getting back to you. You have resolved the issue for me. Ill be sure to get that book!