Grady D
Grady D

Reputation: 2019

Change CSV headers on export

I have the following code,

Get-AdGroup -filter * | select Name, sAMAccountName | Foreach-Object{
    New-Object PSObject -Property @{
        oldAccount = $_.Name
        newAccount = "c:0-.t|adfs-2|" + $_.sAMAccountName

    }
} | Export-CSV "ADGroups.csv" -NoTypeInformation

This works as designed and everything comes out as it should but they do not comeout in the order I need. I believe they come out in alphabetical order so the newAccount is always first. How can I make newAccount the second column?

Upvotes: 0

Views: 83

Answers (2)

Micky Balladelli
Micky Balladelli

Reputation: 9991

You could add a Select-Object prior to the export, that will define the order.

Get-AdGroup -filter * | select Name, sAMAccountName | Foreach-Object{
    New-Object PSObject -Property @{
        oldAccount = $_.Name
        newAccount = "c:0-.t|adfs-2|" + $_.sAMAccountName
    }
} | select oldAccount, newAccount | Export-CSV "ADGroups.csv" -NoTypeInformation

Upvotes: 1

Deadly-Bagel
Deadly-Bagel

Reputation: 1620

Well, you could write your own CSV

"`"oldAccount`",`"newAccount`"" | Out-File "ADGroups.csv" 
Get-AdGroup -filter * | select Name, sAMAccountName | Foreach-Object{
    "`"$_.Name`",c:0-.t|adfs-2|$_.sAMAccountName" | Out-File "ADGroups.csv" -append        
}

Upvotes: 1

Related Questions