SOLVED

Update Azure AD / Microsoft 365 users information - Office Phone numbers from a CSV file

Copper Contributor

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 code (JobTitle, City, Office, etc.).

 

Code:

 

 

 

# Connect to Azure using the Az module
Connect-AzAccount

# Import the CSV file
$csvPath = 'C:\Temp\AzureADUserAttributes.csv'
$users = Import-Csv -Path $csvPath

# Iterate through each user in the CSV and update their information
foreach ($user in $users) {
    $userPrincipalName = $user.UserPrincipalName

    # Retrieve the user using Az module
    $existingUser = Get-AzADUser -UserPrincipalName $userPrincipalName

    # Check if the user exists
    if ($existingUser) {
        # Construct a hashtable of properties to update
        $userProperties = @{}

        # Update properties if they exist in the CSV
        if ($user.JobTitle) { $userProperties['JobTitle'] = $user.JobTitle }
        if ($user.Department) { $userProperties['Department'] = $user.Department }
        if ($user.CompanyName) { $userProperties['CompanyName'] = $user.CompanyName }
        if ($user.City) { $userProperties['City'] = $user.City }
        if ($user.Country) { $userProperties['Country'] = $user.Country }
        if ($user.PostalCode) { $userProperties['PostalCode'] = $user.PostalCode }
        if ($user.State) { $userProperties['State'] = $user.State }
        if ($user.StreetAddress) { $userProperties['StreetAddress'] = $user.StreetAddress }
        if ($user.Office) { $userProperties['Office'] = $user.Office }


        # Update user information only if there are properties to update
        if ($userProperties.Count -gt 0) {
            # Update user information using Az module
            Set-AzADUser -UserPrincipalName $userPrincipalName @userProperties
            Write-Host "User information updated for $userPrincipalName"
        } else {
            Write-Host "No properties to update for $userPrincipalName"
        }
    } else {
        Write-Host "User not found: $userPrincipalName"
    }
}

# Disconnect from Azure session (if needed)
Disconnect-AzAccount

 

 

 

 

However, when I add a new line after line 29 (if ($user.Office) { $userProperties['Office'] = $user.Office })

 

new line

 

if ($user.TelephoneNumber) { $userProperties['TelephoneNumber'] = $user.TelephoneNumber }

 

 

 

 

it throws an error:

 

Update-AzADUser : A parameter cannot be found that matches parameter name 'TelephoneNumber'.
At C:\Path\Update Bulk Azure AD Users information.ps1:36 char:64
+ ... Set-AzADUser -UserPrincipalName $userPrincipalName @userProperties
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Update-AzADUser], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Update-AzADUser 

 

In the CSV File, the column with the phone number is called the same as in the code i.e. "TelephoneNumber".

 

I tried different names:

 

  • OfficeNumber
  • BusinessPhones
  • PhoneNumber
  • Phone

etc., but with each there is the same error.

 

3 Replies

@CzkLTPR 

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#sy... . 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.

best response confirmed by CzkLTPR (Copper Contributor)
Solution

@Harm_Veenstra 

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
    }
}

 

Nice, no problem and glad it worked :)
1 best response

Accepted Solutions
best response confirmed by CzkLTPR (Copper Contributor)
Solution

@Harm_Veenstra 

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
    }
}

 

View solution in original post