Forum Discussion
jebujohn
May 08, 2020Brass Contributor
Bulk update Azure AD with user attributes from CSV
I am looking for a way to update user attributes (OfficePhone and Department) for about 500 users from a CSV to AzureAD using a powershell. Does anyone know of a script that I could use? I am new her...
- May 08, 2020
Hello Jacob,
Your CSV has to look something like this:
UserPrincipalName;Department;TelephoneNumber
manfreddelaat@domain.nl;IT;0135113333
manfred@domain.nl;IT;0622222222Your Powershell code:
# Connect to AzureAD Connect-AzureAD # Get CSV content $CSVrecords = Import-Csv C:\Temp\Test.csv -Delimiter ";" # Create arrays for skipped and failed users $SkippedUsers = @() $FailedUsers = @() # Loop trough CSV records foreach ($CSVrecord in $CSVrecords) { $upn = $CSVrecord.UserPrincipalName $user = Get-AzureADUser -Filter "userPrincipalName eq '$upn'" if ($user) { try{ $user | Set-AzureADUser -Department $CSVrecord.Department -TelephoneNumber $CSVrecord.TelephoneNumber } catch { $FailedUsers += $upn Write-Warning "$upn user found, but FAILED to update." } } else { Write-Warning "$upn not found, skipped" $SkippedUsers += $upn } } # Array skipped users # $SkippedUsers # Array failed users # $FailedUsers
Good luck!
Kind Regards, Manfred de Laat
jpcaid
Nov 27, 2020Copper Contributor
yea i did this on azure ad
sunJeezy
Nov 27, 2020Copper Contributor
I just tried loading up powershell and typing,
Connect-AzureAD
Get-ADUser
Received the following error:
Get-ADUser : The term 'Get-ADUser' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ Get-ADUser
+ ~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-ADUser:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Connect-AzureAD
Get-ADUser
Received the following error:
Get-ADUser : The term 'Get-ADUser' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ Get-ADUser
+ ~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-ADUser:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
- jebujohnAug 18, 2021Brass ContributorTry a csv with UserPrincipalName, Manager where both are UPNs. The UserPrincipalName for the user whom you want to assign a manager and under the manager the UPN of the manager
- vipfafenAug 18, 2021Copper ContributorThanks for the quick response, but I run into mistakes. My CSV is structured as follows:
UserPrincipalName,"ManagerName"
<Username.username>#EXT#@<domain>,"<Managername>, <Managersurname>"
<username.username>@<domain2>,"<Managername>, <Managersurname>"
... - jebujohnAug 18, 2021Brass Contributor
$CSVrecords = Import-Csv test.csv -Delimiter "," foreach ($CSVrecord in $CSVrecords) { $usr = $CSVrecord.UserPrincipalName $manager = $CSVrecord.Manager $user = Get-AzureADUser -Filter "userPrincipalName eq '$usr'" $Managerobj = Get-AzureADUser -Filter "userprincipalname eq '$manager'" if ($user) { try{ $user | Set-AzureADUserManager -RefObjectid $Managerobj.objectid } catch { $FailedUsers += $usr Write-Warning "$usr user found, but FAILED to update." } } else { Write-Warning "$usr not found, skipped" $SkippedUsers += $usr } }
- vipfafenAug 18, 2021Copper Contributor@arnelianfox did you get a working script? Can't get it to work either
- carnelianfoxMar 02, 2021Copper Contributor
Jacob John could you please write out the script in full. It's a bit confusing the way you put it. I am unable to correctly update User Manager. Thanks,
- Jacob JohnNov 28, 2020Copper Contributor
The appropriate command is set-azureadusermanager
you will need the azureAD objectid of the manager, which we will have to first call. So this script will not work as is.
$user=$CSVrecord.UserPrinicipalName
$Manager= $CSVrecord.Manager
$ManagerObj=Get-AzureADUser -Objectid $Manager
Set-AzureADUserManager -ObjectId $User -RefObjectId $ManagerObj.ObjectId
In your specific instance, it may be simple to populate the csv with the objectid of the single manager or the script with the objectid as only one manager exists.
- sunJeezyNov 27, 2020Copper ContributorThere are no spaces in the CSV. I've modified the script a bunch of times in order to update all the fields for my 1500 users, which worked flawlessly. The manager piece is what I am having a challenge with.
I already have the AzureAD module installed. I've already updated all the other fields with no issues.
Going back to the documentation for Set-AzureADUser (https://docs.microsoft.com/en-us/powershell/module/azuread/set-azureaduser?view=azureadps-2.0) I do not see "Manager" listed as a parameter. - RhysButlerNov 27, 2020Copper Contributor
sunJeezyYou need to install the AzureModule (install-module azuread)
- sunJeezyNov 27, 2020Copper Contributor
The Azure_manager.csv file contains userprincipalname;manager
# Connect to AzureADConnect-AzureAD# Get CSV content$CSVrecords = Import-Csv C:\Users\k4rna\Desktop\azureinfocorrection\azure_manager.csv -Delimiter ";"# Create arrays for skipped and failed users$SkippedUsers = @()$FailedUsers = @()# Loop trough CSV recordsforeach ($CSVrecord in $CSVrecords) {$upn = $CSVrecord.UserPrincipalName$user = Get-AzureADUser -Filter "userPrincipalName eq '$upn'"if ($user) {try{$user | Set-AzureADUser -Manager $CSVrecord.Manager} catch {$FailedUsers += $upnWrite-Warning "$upn user found, but FAILED to update."}}else {Write-Warning "$upn not found, skipped"$SkippedUsers += $upn}}# Array skipped users# $SkippedUsers# Array failed users# $FailedUsers