Forum Discussion

jebujohn's avatar
jebujohn
Copper Contributor
May 08, 2020

Bulk update Azure AD with user attributes from CSV

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 her...
  • Manfred101's avatar
    May 08, 2020

    jebujohn 

     Hello Jacob,

     

    Your  CSV has to look something like this:

    UserPrincipalName;Department;TelephoneNumber
    manfreddelaat@domain.nl;IT;0135113333
    manfred@domain.nl;IT;0622222222

     

    Your Powershell code:

     

    # Connect to AzureAD
    Connect-AzureAD
    
    # Get CSV content
    $CSVrecords = Import-Csv C:\Temp\Test.csv -Delimiter ";"
    
    # Create arrays for skipped and failed users
    $SkippedUsers = @()
    $FailedUsers = @()
    
    # Loop trough CSV records
    foreach ($CSVrecord in $CSVrecords) {
        $upn = $CSVrecord.UserPrincipalName
        $user = Get-AzureADUser -Filter "userPrincipalName eq '$upn'"  
        if ($user) {
            try{
            $user | Set-AzureADUser -Department $CSVrecord.Department -TelephoneNumber $CSVrecord.TelephoneNumber
            } catch {
            $FailedUsers += $upn
            Write-Warning "$upn user found, but FAILED to update."
            }
        }
        else {
            Write-Warning "$upn not found, skipped"
            $SkippedUsers += $upn
        }
    }
    
    # Array skipped users
    # $SkippedUsers
    
    # Array failed users
    # $FailedUsers

     

     

    Good luck!

     

    Kind Regards, Manfred de Laat

Resources