Graph script updates first user then fails every user after first User 'username@user' not found

Steve Brophy 5 Reputation points
2025-03-18T16:54:00.4733333+00:00

csv file is 2 columns UserPrincipalName and telephoneNumber

Connect to Microsoft Graph

Connect-MgGraph -Scopes User.ReadWrite.All

Read the CSV file

$users = Import-Csv -Path "C:\temp\2Phone.csv"

Go through each user in the CSV and update the BusinessPhone

foreach ($user in $users) {

$userPrincipalName = $user.UserPrincipalName

$BusinessPhone = $user.BusinessPhone

# Check if the user exists

$existingUser = Get-MgUser -UserId $userPrincipalName -Property UserPrincipalName, BusinessPhone -ErrorAction SilentlyContinue | Select-Object UserPrincipalName, BusinessPhone

if ($existingUser) {

    # Check if the existing BusinessPhone matches the new value

    if ($existingUser.BusinessPhone -eq $BusinessPhone) {

        # BusinessPhone already set with the same value

        Write-Host "User '$userPrincipalName' already has BusinessPhone '$BusinessPhone'." -ForegroundColor Cyan

    }

    else {

        # Update the BusinessPhone

        Update-MgUser -UserId $userPrincipalName -BusinessPhone $BusinessPhone

        Write-Host "User '$userPrincipalName' updated BusinessPhone to '$BusinessPhone' successfully." -ForegroundColor Green

    }

}

else {

    # User not found

    Write-Host "User '$userPrincipalName' not found." -ForegroundColor Red

}

}

Microsoft Security Microsoft Graph
{count} votes

1 answer

Sort by: Most helpful
  1. SrideviM 5,630 Reputation points Microsoft External Staff Moderator
    2025-03-25T02:12:02.8733333+00:00

    Hello Steve Brophy,

    In my case, I have created one sample csv file named 2Phone.csv with data as below:

    
    UserPrincipalName,telephoneNumber
    
    ******@xxxxxxxxxx.onmicrosoft.com,9014xxx489
    
    ******@xxxxxxxxxx.onmicrosoft.com,123-4x6-7890
    
    ******@xxxxxxxxx.onmicrosoft.com,987-xx4-3210
    
    ******@xxxxxxxxx.onmicrosoft.com,555-1x3-4567
    
    ******@xxxxxxxxxxxxx.onmicrosoft.com,96xxxx9648
    
    

    To update the BusinessPhones property of these users, you can make use below modified PowerShell script that worked for me:

    # Connect to Microsoft Graph with required permissions
    Connect-MgGraph -Scopes "User-Phone.ReadWrite.All "
    
    # Read the CSV file
    $users = Import-Csv -Path "C:\temp\2Phone.csv"
    
    # Loop through each user in the CSV and update their BusinessPhone
    foreach ($user in $users) {
    
        # Trim to remove any extra spaces
        $userPrincipalName = $user.UserPrincipalName.Trim()
        $BusinessPhone = $user.telephoneNumber.Trim()
    
        # Check if the user exists in Microsoft 365
        $existingUser = Get-MgUser -UserId $userPrincipalName -Property "UserPrincipalName", "BusinessPhones" -ErrorAction SilentlyContinue |
            Select-Object UserPrincipalName, @{Name="BusinessPhone"; Expression={$_.BusinessPhones -join ", "}}
    
        if ($existingUser) {
            # Check if the existing BusinessPhones match the new value
            if ($existingUser.BusinessPhone -eq $BusinessPhone) {
                Write-Host "User '$userPrincipalName' already has BusinessPhone '$BusinessPhone'." -ForegroundColor Cyan
            } else {
                # Update the BusinessPhones property (as an array)
                Update-MgUser -UserId $userPrincipalName -BusinessPhones @($BusinessPhone)
                Write-Host "User '$userPrincipalName' updated BusinessPhone to '$BusinessPhone' successfully." -ForegroundColor Green
            }
        } else {
            # User not found in Microsoft 365
            Write-Host "User '$userPrincipalName' not found." -ForegroundColor Red
        }
    
        # Sleep to prevent rate-limiting issues
        Start-Sleep -Seconds 1
    }
    

    Response:

    enter image description here

    To confirm that, I checked "SriTest" user properties where BusinessPhones value update successfully as below:

    enter image description here

    Hope this helps!


    If this answer was helpful, please click "Accept the answer" and mark Yes, as this can be beneficial to other community members.

    User's image

    If you have any other questions or still running into more issues, let me know in the "comments" and I would be happy to help you.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.