Powershell 在ForEach循环后导出CSV

Powershell 在ForEach循环后导出CSV,powershell,active-directory,export-to-csv,Powershell,Active Directory,Export To Csv,我试图将这些Active Directory用户(在创建之后)导出到CSV中,但我认为我做得不对。我能得到一些帮助吗?我需要将samAccountName和SID导出到CSV文件,但似乎无法导出任何内容 Import-Module ActiveDirectory Import-Csv "C:\Users\Administrator\Desktop\GroupProject3Usernames2.csv" | ForEach-Object { $userPrincipal = $_."UserN

我试图将这些Active Directory用户(在创建之后)导出到CSV中,但我认为我做得不对。我能得到一些帮助吗?我需要将samAccountName和SID导出到CSV文件,但似乎无法导出任何内容

Import-Module ActiveDirectory
Import-Csv "C:\Users\Administrator\Desktop\GroupProject3Usernames2.csv" | ForEach-Object {
 $userPrincipal = $_."UserName" + "mycorp.com"
New-ADUser $userPrincipal `
 -SamAccountName  $_."UserName" `
 -givenname $_."FirstName" `
 -surname $_."LastName" `
 -AccountPassword (ConvertTo-SecureString "Password1" -AsPlainText -Force) `
 -ChangePasswordAtLogon $true  `
 -Enabled $true
Add-ADGroupMember "Domain Admins" $_."UserName";
Write-Host $_."UserName" " Created"
} | out-file "C:\Users\Administrator\Desktop\output.csv" -SamAccountName -SID

你并不是真的要把任何东西放到文件里去。删除管道输出文件并将写入主机替换为:
“$($.UserName)创建了”| out file“your file”-append

如何将SID也放在另一列中?提前谢谢!将New ADuser的结果存储到名为$newUser的变量中。然后在写主机中包含“$($newUser.SID)”。