Forum Discussion
CzkLTPR
Nov 23, 2023Copper Contributor
Update Azure AD / Microsoft 365 users information - Office Phone numbers from a CSV file
Hello, I am trying to update the Office Phone numbers of my Microsoft 365 users using a list in a CSV file. Generally, the code works - it successfully updates all other attributes that are in the ...
- 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 } }
CzkLTPR
Nov 23, 2023Copper Contributor
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, 2023
Nice, no problem and glad it worked 🙂