Forum Discussion
Update Azure AD / Microsoft 365 users information - Office Phone numbers from a CSV file
- Nov 23, 2023
This is what I was looking for. Thank you.
I had to edit the author's script by changing "MgBetaUser" to "Get-MgUser". After this change it works as expected.
In addition, I added:
# Check if BusinessPhones is empty if ([string]::IsNullOrEmpty($BusinessPhones)) { Write-Host "BusinessPhones is empty for user '$userPrincipalName'. Skipping update." -ForegroundColor Yellow continue # Skip to the next user in the loop }
Working code to update multiple user business phones:
# Connect to Microsoft Graph Connect-MgGraph -Scope User.ReadWrite.All # Read the CSV file $users = Import-Csv -Path "C:\Temp\AzureADUserAttributes.csv" # Go through each user in the CSV and update the BusinessPhones foreach ($user in $users) { $userPrincipalName = $user.UserPrincipalName $BusinessPhones = $user.BusinessPhones # Check if BusinessPhones is empty if ([string]::IsNullOrEmpty($BusinessPhones)) { Write-Host "BusinessPhones is empty for user '$userPrincipalName'. Skipping update." -ForegroundColor Yellow continue # Skip to the next user in the loop } # Check if the user exists $existingUser = Get-MgUser -UserId $userPrincipalName -ErrorAction SilentlyContinue if ($existingUser) { # Check if the existing BusinessPhones matches the new value if ($existingUser.BusinessPhones -eq $BusinessPhones) { # BusinessPhones already set with the same value Write-Host "User '$userPrincipalName' already has BusinessPhones '$BusinessPhones'." -ForegroundColor Cyan } else { # Update the BusinessPhones Update-MgUser -UserId $userPrincipalName -BusinessPhones $BusinessPhones Write-Host "User '$userPrincipalName' updated BusinessPhones to '$BusinessPhones' successfully." -ForegroundColor Green } } else { # User not found Write-Host "User '$userPrincipalName' not found. BusinessPhones field is empty." -ForegroundColor Yellow } }
The parameter is not there if I check the documentation here: https://learn.microsoft.com/en-us/powershell/module/az.resources/update-azaduser?view=azps-11.0.0#syntax . It's beter to start using Microsoft Graph cmdlets for this, excellent article here https://o365info.com/update-azure-ad-users/ (Which uses Update-MgBetaUser)
Please click Mark as Best Response & Like if my post helped you to solve your issue.
This will help others to find the correct solution easily. It also closes the item.
If one of the posts was helpful in other ways, please consider giving it a Like.
This is what I was looking for. Thank you.
I had to edit the author's script by changing "MgBetaUser" to "Get-MgUser". After this change it works as expected.
In addition, I added:
# Check if BusinessPhones is empty
if ([string]::IsNullOrEmpty($BusinessPhones)) {
Write-Host "BusinessPhones is empty for user '$userPrincipalName'. Skipping update." -ForegroundColor Yellow
continue # Skip to the next user in the loop
}
Working code to update multiple user business phones:
# Connect to Microsoft Graph
Connect-MgGraph -Scope User.ReadWrite.All
# Read the CSV file
$users = Import-Csv -Path "C:\Temp\AzureADUserAttributes.csv"
# Go through each user in the CSV and update the BusinessPhones
foreach ($user in $users) {
$userPrincipalName = $user.UserPrincipalName
$BusinessPhones = $user.BusinessPhones
# Check if BusinessPhones is empty
if ([string]::IsNullOrEmpty($BusinessPhones)) {
Write-Host "BusinessPhones is empty for user '$userPrincipalName'. Skipping update." -ForegroundColor Yellow
continue # Skip to the next user in the loop
}
# Check if the user exists
$existingUser = Get-MgUser -UserId $userPrincipalName -ErrorAction SilentlyContinue
if ($existingUser) {
# Check if the existing BusinessPhones matches the new value
if ($existingUser.BusinessPhones -eq $BusinessPhones) {
# BusinessPhones already set with the same value
Write-Host "User '$userPrincipalName' already has BusinessPhones '$BusinessPhones'." -ForegroundColor Cyan
}
else {
# Update the BusinessPhones
Update-MgUser -UserId $userPrincipalName -BusinessPhones $BusinessPhones
Write-Host "User '$userPrincipalName' updated BusinessPhones to '$BusinessPhones' successfully." -ForegroundColor Green
}
}
else {
# User not found
Write-Host "User '$userPrincipalName' not found. BusinessPhones field is empty." -ForegroundColor Yellow
}
}
- Nov 23, 2023Nice, no problem and glad it worked 🙂