Answered by:
get-aduser using upn

Question
-
hi,
i am trying to bulk update all the AD users in the company with their department and job title i have this all in a CSV.
but the issues i am having is i only have their UPN to lookup this is 2000+ users so i don't want to find all their SAM accounts names and match everything up.
any help would be great.
Thanks
josh
Tuesday, June 20, 2017 2:07 PM
Answers
-
Import-Csv <pathToFile> | ForEach {
Added the WhatIf switch so it can be tested without making the changes, remove if satisfied
$csv = $_
Get-ADUser -Filter "UserPrincipalName -eq '$($csv.UPN)'" |
Set-ADUser -Replace @{Department="$($csv.Department)";Title="$($csv.Title)"} -WhatIf
}
If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''
Tuesday, June 20, 2017 2:28 PM
All replies
-
Import-Csv <pathToFile> | ForEach {
Added the WhatIf switch so it can be tested without making the changes, remove if satisfied
$csv = $_
Get-ADUser -Filter "UserPrincipalName -eq '$($csv.UPN)'" |
Set-ADUser -Replace @{Department="$($csv.Department)";Title="$($csv.Title)"} -WhatIf
}
If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''
Tuesday, June 20, 2017 2:28 PM -
hi,
thanks for this
i get this output if i run the cmdlet:
Set-ADUser : A parameter cannot be found that matches parameter name 'Filter'.
At line:1 char:42
+ Import-Csv C:\Temp\TEST.CSV | Set-ADUser -Filter "UserPrincipalName -eq
'$($_.UP ...
+ ~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-ADUser], ParameterBind
ingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.ActiveDirectory
.Management.Commands.SetADUserTuesday, June 20, 2017 2:32 PM -
My apolgies, Set-ADUser does not have a Filter, so I changed the script to pipe to Get-ADUser first, then pipe over to Set-ADUser. Please also note, my properites for the header names may not be the same as yours.
If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''
Tuesday, June 20, 2017 2:44 PM -
thanks that worked perfectly when i was writing my own i was doing the same creating with get-aduser then changing to set and couldn't work out why it didn't work.
but now you have explained that filter does not work with set-aduser it makes sense.
Thanks josh
Tuesday, June 20, 2017 2:55 PM -
Hello,
You would have to add the column header as a property of the variable. For instance, if your CSV column headers are named "UPN", "Department" and "Title", your command would look like this:
Import-csv c:\Temp\TEST.csv | %{get-aduser $_.UPN | set-aduser -department $_.department -title $_.title }
Dan
Tuesday, June 20, 2017 3:04 PM -
Hello,
You would have to add the column header as a property of the variable. For instance, if your CSV column headers are named "UPN", "Department" and "Title", your command would look like this:
Import-csv c:\Temp\TEST.csv | %{get-aduser $_.UPN | set-aduser -department $_.department -title $_.title }
Dan
UPN cannot be used this way which is why the original question was asked. It must be gotten with a filter clause.
\_(ツ)_/
Tuesday, June 20, 2017 3:07 PM -
Correction. The "get-aduser" commandlet needs a filter. Get-qaduser does not need a filter. My apologies.Tuesday, June 20, 2017 3:53 PM