Reputation: 243
At the end of this project, I am trying to use a CSV file to update users personal details within AD. I plan to use Get-ADUser piped into Set-ADUser
My current problem is returning the user records from AD using Get-ADUser
My CSV file is as follows
FirstName,LastName,DisplayName,CompanyName,EmailAddress
John,Doe,John Doe,Test Company,[email protected]
Jane,Doe,Jane Doe,Test Company,[email protected]
My PowerShell script looks like this
Import-Csv .\Users.csv | ForEach {
Write-Host $_.EmailAddress
Write-Host $_.DisplayName
"`"$($_.EmailAddress)`""
Get-ADUser -Filter { userPrincipalName -eq "`"$($_.EmailAddress)`""}
}
What is returned is
[email protected]
John Doe
"[email protected]"
[email protected]
Jane Doe
"[email protected]"
Running Get-ADUser without using variables like below, returns the user account as it should, whereas the above code does not.
Get-ADUser -Filter { userPrincipalName -eq "[email protected]"}
Where am I going wrong?
Upvotes: 0
Views: 411
Reputation: 525
The $_ is a variable for the current value in the pipe line or current object that the cmd-let returns. When you use $_ in Get-ADUser, it $_ will be the object returned from Get-ADUser cmd-let. Just create a variable and assign $_.EmailAddress inside foreach loop and pass this variable to filter.
Import-Csv .\Users.csv | ForEach {
$emailAddress = $_.EmailAddress
Write-Host $_.EmailAddress
Write-Host $_.DisplayName
Get-ADUser -Filter { userPrincipalName -eq $emailAddress }
}
Upvotes: 1