Forum Discussion

jebujohn's avatar
jebujohn
Brass Contributor
May 08, 2020
Solved

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 here and if I have not given enough information, please let me know. I tried using Set-AzureADUser piping records using a foreach statement from a csv that I imported, but it was throwing up errors. 

Thanks!

Jacob

72 Replies

  • Arun_Siwakoti's avatar
    Arun_Siwakoti
    Copper Contributor
    Simple to see and learn this PowerShell code start with exchangeonline first after that we go through AzureAD: Please email me: email address removed for privacy reasons 🙂

    Import-Module ExchangeOnlineManagement
    Install-Module ExchangeOnlineManagement
    Connect-ExchangeOnline
    $CSVrecords = Import-Csv C:\newdata.csv
    $total = $CSVrecords.Count
    $i = 0
    $newdom = "your.domian.com" #type vefied domain from your domain list
    foreach ($user in $CSVrecords)
    {
    $i = $i + 1
    $oid = $user.ObjectId
    $username = $user.GivenName.ToLower() + "." + $user.Surname.ToLower() #ToLower is to create all character in lowercase ToUpper() for uppercase
    #Write-Host "$username"
    $finalemail = $username + "@" + $newdom
    #Write-Host "$finalemail"
    Set-Mailbox $oid -EmailAddresses $finalemail
    Write-Progress -activity "Processing $user.DisplayName $i out of $total completed"

    }
    Write-Host "All UPN set to domain $newdom"

    Now Again time for AzureAD

    Install-Module AzureAD
    import-Module AzureAD
    Connect-AzureAD
    $CSVrecords = Import-Csv C:\Users\aruns\OneDrive\Desktop\newdata.csv
    $total = $CSVrecords.count
    $i = 0
    $pdomain = "your.domain.com" #New tetantdoamin here betweeen " "
    # Loop trough CSV records
    foreach ($user in $CSVrecords)
    {
    $i = $i + 1
    $oid = $user.ObjectId
    $username = $user.GivenName.ToLower() + "." + $user.Surname.ToLower()
    #Write-Host "$username"
    $finalemail = $username + "@" + $newdom
    #Write-Host "$finalemail"
    Set-AzureADUser -ObjectId $oid -UserPrincipalName $finalemail
    Write-Progress -activity "Processing $user.DisplayName $i out of $total completed"
    }
    Write-Host "Well done Action Completed!"

    If helpful I love you all
    • Kevin_Morgan's avatar
      Kevin_Morgan
      Iron Contributor

      dnelsonazpain 

       

      Hi, You have to use the ExtensionProperty to set the null value or clear the property with Set-AzureADUser cmdlet.

       

      $properties = [Collections.Generic.Dictionary[[String],[String]]]::new()
      $properties.Add("Mobile", [NullString]::Value)
      Set-AzureADUser -ObjectId "email address removed for privacy reasons" -ExtensionProperty $properties
      
      # Refer to the below post for more details.
      # https://morgantechspace.com/2022/03/update-bulk-azure-ad-user-attributes-using-powershell.html

       

       

      Check this post for more details: https://morgantechspace.com/2022/09/remove-or-clear-property-or-set-null-value-using-set-azureaduser-cmdlet.html 

       

      Check out https://specmasoft.com/office-365-manager/update-bulk-azure-ad-users-in-microsoft-365-using-a-csv-file from https://specmasoft.com/office-365-manager to update bulk user attributes, including setting a manager, updating licenses, and managing extension attributes (e.g., employeeId). You can also set null values or clear existing property values. Additionally, the tool allows you to update passwords for bulk users and add multiple users to any groups or teams.

       

      https://specmasoft.com/office-365-manager/update-bulk-azure-ad-users-in-microsoft-365-using-a-csv-file

       

      • rogerdodger's avatar
        rogerdodger
        Copper Contributor
        so any other set attribute does not get touched at all, correct? I"m only looking to update Titles and plan on testing this but obv don't want anything else to change on the users. . .

        # 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 -JobTitle $CSVrecord.JobTitle
        } 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

Resources