Script repository

Force Active Directory synchronization with Microsoft 365

Updated on: Jan 18, 2026, Views: 15115

Microsoft 365

The scripts force Active Directory synchronization with Microsoft 365. You can use the scripts as part of a business rules, custom command or scheduled task.

Before using the scripts, make sure that the computer where Adaxes service is installed and the computer where the DirSync / AAD Connect tool is installed allow execution of remote PowerShell commands. For details, see About Remote Requirements.

Script 1: Using the DirSync tool

Parameters

  • $dirsyncServer - the fully-qualified domain name of server where the DirSync tool is installed.
  • $dirsyncmodulePath - the path to the DirSync module relative to the computer where it is installed (by default, the path is C:\Program Files\Windows Azure Active Directory Sync\DirSyncConfigShell.psc1).
$dirsyncServer = "dirsync.domain.com" # TODO: modify me

$scriptBlock = {
    $dirsyncModulePath = "C:\Program Files\Windows Azure Active Directory Sync\DirSyncConfigShell.psc1" # TODO: modify me

    powershell -PSConsoleFile $dirsyncModulePath -Command "Start-OnlineCoexistenceSync"
}

$result = Invoke-Command -ComputerName $dirsyncServer -ScriptBlock $scriptBlock

if ($result -ne $NULL)
{
    $Context.LogMessage($result, "Warning")
}

Script 2: Using the AAD Connect tool

In the script, the $dirsyncServer variable specifies the fully-qualified domain name of server where the AAD Connect tool is installed.

$dirsyncServer = "dirsync.domain.com" # TODO: modify me

Invoke-Command -ComputerName $dirsyncServer -ErrorAction Stop -ScriptBlock {
    Import-Module "C:\Program Files\Microsoft Azure AD Sync\Bin\ADSync\ADSync.psd1"
        
    Start-ADSyncSyncCycle -PolicyType Delta
}

Comments 7

You must be signed in to comment.

  • Christian Diaz

    Christian Diaz

    This script/command will error if there's already a synchronization in progress, which injects an error in the Adaxes logging. Would be nice to see some error handling in this or maybe a check beforehand execution if a sync is already running.

    • Support

      Support

      Hello,

      Unfortunately, there is no such possibility as it would require parsing the error returned by the Invoke-Command cmdlet.

      • Christian

        Christian

        Had the same issue , not sure if something like this would help you? Not tested yet in Adaxes

        $O365SyncServer = "server.domain.tld" #Server used for DirSync Office 365  
        $var_syncDone = 1  
        $timeOut = 180  
          
        do{  
         $var_syncDone = 1  
          
         try {  
         Invoke-Command -ComputerName $O365SyncServer -ScriptBlock {  
         Import-Module adsync  
         Start-ADSyncSyncCycle -PolicyType Delta  
         } -ErrorAction Stop | Out-Null  
          
         Write-Host "Starting O365 Directory Sync on server $O365SyncServer"   
         } catch {  
         Write-Host "Error O365 Directory Sync on server $O365SyncServer due to:`r`n$($_.Exception.Message)`r`n"  
         $var_syncDone = 0  
         Write-Host "Waiting $($timeOut) seconds ..."  
         Start-Sleep -Seconds $timeOut  
         }  
        }while($var_syncDone -eq 0)
        
  • Christian

    Christian

    You define $dirsyncServer but use $computerName later : )

    • Support

      Support

      Hello Christian,

      -ComputerName is a predefined parameter name for the Invoke-Command cmdlet. There is no possibility to change it. We use $dirsyncServer as the corresponding tools are usually installed on server editions.

      • Christian

        Christian

        Hi,

        I talking about the first script - if you run it, it will throw error , as variable $computerName was never set

        $dirsyncServer = "dirsync.domain.com" # TODO: modify me  
        $result = Invoke-Command -ComputerName $computerName -ScriptBlock $scriptBlock
        
        • Support

          Support

          Hello Christian,

          Thank you for pointing out the mistake. We have just fixed it.

Got questions?

Support Questions & Answers

We use cookies to improve your experience.
By your continued use of this site you accept such use.
For more details please see our privacy policy and cookies policy.