May 08 2020 08:22 AM
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 here and if I have not given enough information, please let me know. I tried using Set-AzureADUser piping records using a foreach statement from a csv that I imported, but it was throwing up errors.
Thanks!
Jacob
Mar 01 2021 04:40 PM
@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,
Apr 15 2021 04:28 PM - edited Apr 15 2021 04:44 PM
@Manfred101 This worked great!
One think im stuggling with, is the user doesnt get updated when one of the properties is empty? is there a way around that? i.e. the mobile number for that user is empty in the csv most of the update for that user fails.
Apr 16 2021 06:21 AM
@esfitzI got around this by entering a - (dash) into blank fields. Otherwise, you are right, the script will stop there.
May 11 2021 08:27 AM
@wllrkn Thanks bunch, removing this fixed the issue for me :).
Jun 09 2021 08:17 PM
Aug 18 2021 09:39 AM
Aug 18 2021 10:42 AM - edited Aug 19 2021 05:51 PM
$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
}
}
Aug 18 2021 11:52 AM
Aug 18 2021 12:10 PM
Aug 18 2021 01:03 PM
Sep 21 2021 08:21 PM
This is the shortest line to get the job done:
Assuming your csv file has two columns;; UserPrincipalName and a column with the attribute you wish to update ( the user's Title for example); assuming the file is stored in the D drive and is titled updateusers, then here's you code:
Import-CSV D:\updateusers.csv | ForEach-Object { Set-AzureADUser -ObjectID $_.UserPrincipalName -Title $_.Title }
If you want to update more attributes you can add them to the code, for example to update department just add -Department $_.Department to the code above:
Import-CSV D:\updateusers.csv | ForEach-Object { Set-AzureADUser -ObjectID $_.UserPrincipalName -Title $_.Title -Department $_.Department }
Sep 21 2021 08:40 PM
Nov 18 2021 02:52 AM
Apr 01 2022 06:30 AM
i did it like That:
# Loop trough CSV records
foreach ($CSVrecord in $CSVrecords) {
$upn = $CSVrecord.UserPrincipalName
$upnmanager = $CSVrecord.Manager
$user = Get-AzureADUser -Filter "UserPrincipalName eq '$upn'"
$Manager = Get-AzureADUser -Filter "UserPrincipalName eq '$upnmanager'"
if ($user) {
try{
$user | Set-AzureADUser -City $CSVrecord.City -CompanyName $CSVrecord.CompanyName -Department $CSVrecord.Department -StreetAddress $CSVrecord.StreetAddress -PostalCode $CSVrecord.PostalCode -JobTitle $CSVrecord.JobTitle -Mobile $CSVrecord.Mobile -PhysicalDeliveryOfficeName $CSVrecord.PhysicalDeliveryOfficeName
Set-AzureADUserManager -ObjectId $user.ObjectId -RefObjectId $Manager.objectId
} catch {
$FailedUsers += $upn
Write-Warning "$upn user found, but FAILED to update."
}
}
else {
Write-Warning "$upn not found, skipped"
$SkippedUsers += $upn
}
}
Thanks au lot to all of you !
Apr 13 2022 12:14 AM
Apr 13 2022 02:15 AM
you should add a manager field in our CSV with thier UPN
and add these on the loop
$upnmanager = $CSVrecord.Manager
$Manager = Get-AzureADUser -Filter "UserPrincipalName eq '$upnmanager'"
if ($user) {
try{
Set-AzureADUserManager -ObjectId $user.ObjectId -RefObjectId $Manager.objectId
}
Apr 22 2022 12:25 AM - edited Apr 22 2022 12:45 AM
@Manfred101 - The below post shares the PowerShell script to modify bulk user attributes for multiple user accounts in a simple way by importing user details from a CSV file.
https://morgantechspace.com/2022/03/update-bulk-azure-ad-user-attributes-using-powershell.html
This script helps to update bulk user attributes as hash table in single command execution.
#Hashtable to keep multiple attribute values
$AttributesToUpdate = @{}
$AttributesToUpdate["JobTitle"] = "Sales Manager"
$AttributesToUpdate["Department"] = "Sales"
# Set required user attributes.
# Need to prefix the variable AttributesToUpdate with @ symbol instead of $ to pass hashtable as parameters (ex: @AttributesToUpdate).
Set-AzureADUser -ObjectId "user@domain.com" @AttributesToUpdate
# Refer to the below post for more details.
# https://morgantechspace.com/2022/03/update-bulk-azure-ad-user-attributes-using-powershell.html
May 12 2022 04:22 AM
Jul 18 2022 08:28 AM
Jan 14 2023 04:48 PM
@Manfred101 Thanks for this. I know this is old but, if you are still here, how long should it take to show up in Azure AD.