导出时更改 CSV headers

Change CSV headers on export

我有以下代码,

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

这按设计工作,一切正常,但它们没有按照我需要的顺序出现。我相信它们是按字母顺序出现的,所以 newAccount 总是排在第一位。如何让 newAccount 成为第二列?

好吧,您可以编写自己的 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        
}

您可以在导出之前添加一个 Select-Object,这将定义顺序。

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