Powershell script to loop through MSOLUsers in CSV file until they are synced to Azure AD/Entra ID

mark terry 45 Reputation points
2024-05-30T16:43:46.4033333+00:00

Hi all,

I have an input CSV file like this:

UserPrincipalName

user1@test.com

user2@test.com

These users are located in our On Premise Active Directory but are NOT synced to our Azure AD/Entra ID (they are in an OU which we exclude from syncing to Azure AD/Entra ID).

Occasionally, we need to be able to sync these users to Azure AD/Entra ID. We do this by simply moving the users out of the "Do Not Sync" OU. I would like to have a PowerShell script so that when it is run, it will check to see when the users have been synced to Azure AD/Entra ID. The script requirements would be:

  1. Read the contents of the CSV file.
  2. Create a loop so that the script will check if the users have synced to Azure AD/Entra ID.
  3. Once all users in the CSV file have been confirmed that they have been synced to Azure AD/Entra ID, exit the loop.

What I have right now, is a foreach iteration which will read in my input file and check if the MSOLusers exist or not in Azure AD/Entra ID:

$SyncedUsers = Import-Csv D:\Users.csv | Select UserPrincipalName

foreach ($User in $SyncedUsers){

Get-MsolUser -UserPrincipalName $User.UserPrincipalName

}

The above will return an error if the users are not in Azure AD/Entra ID, and will return the MSOLuser if they are in Azure AD/Entra ID. I think I need a "wrapper" loop to check on the status of the Get-MsolUser -UserPrincipalName $User.UserPrincipalName command and to keep looping until all users in the CSV file are synced to Azure AD/Entra ID, but don't know how to incorporate the while loop part!

Thanks in advance!

Microsoft Exchange Online Management
Microsoft Exchange Online Management
Microsoft Exchange Online: A Microsoft email and calendaring hosted service.Management: The act or process of organizing, handling, directing or controlling something.
4,314 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,223 questions
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
20,139 questions
0 comments No comments
{count} votes

Accepted answer
  1. Mike Hu-MSFT 1,560 Reputation points Microsoft Vendor
    2024-05-31T05:44:37.2833333+00:00

    Hi mark terry,

    Please understand that the tag “Microsoft Exchange Online Management” is for general questions related to Exchange Online. For your question about the script which is not supported in our community.

    But based on my personal experience, you could try the following script :

    $syncedUsers = Import-Csv D:\Users.csv | Select UserPrincipalName
    $allUsersSynced = $false
    $retryInterval = 60 # seconds to wait before the next retry
    while (-not $allUsersSynced) {
        $allUsersSynced = $true # Assume all users are synced, and prove otherwise if a user isn't found
        foreach ($user in $syncedUsers) {
            try {
                Get-MsolUser -UserPrincipalName $user.UserPrincipalName -ErrorAction Stop
                Write-Host "User $($user.UserPrincipalName) is synced."
            }
            catch {
                # If an error is caught, it means the user is not synced yet.
                Write-Host "User $($user.UserPrincipalName) is not synced yet."
                $allUsersSynced = $false
            }
        }
        
        if (-not $allUsersSynced) {
            # Wait for some time before rechecking
            Write-Host "Waiting for $retryInterval seconds before rechecking..."
            Start-Sleep -Seconds $retryInterval
        }
    }
    Write-Host "All users are now synced."
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful