Reputation: 53
I have a csv file with employee ID's, these ID's are tied to AD Accounts, I need to get the AD User information from the employee ID and export them to a csv file. Below is the script I wrote, it works if I run it without the export-csv switch, I have tried adding a pipe but the error I get from that is pipe cannot be empty.
$userIDs = Import-CSV "C:\Users_.csv"
foreach($userID in $userIDs)
{
Get-ADUser -Filter "EmployeeID -eq $($userID.EmployeeID)" -Properties SAMAccountName
}
Export-CSV "C:\UserResults.csv" -NoTypeInformation -Encoding UTF8
Upvotes: 2
Views: 14836
Reputation: 437111
You can use a single pipeline:
Import-CSV "C:\Users_.csv" |
ForEach-Object { Get-ADUser -Filter "EmployeeID -eq $($_.EmployeeID)" } |
Export-CSV "C:\UserResults.csv" -NoTypeInformation -Encoding UTF8
Note the use of the ForEach-Object
cmdlet instead of the foreach
statement - only the cmdlet can be used in a pipeline.
(Somewhat confusingly, the cmdlet has an alias also named foreach
; whether the statement or the cmdlet is used is implied by the context (parsing mode).)
Inside the script block { ... }
passed to ForEach-Object
, automatic variable $_
represents the input object at hand.
Also note that Get-ADUser
outputs objects that have property SAMAccountName
by default, so you needn't request it explicitly.
Upvotes: 3