PowerShell password sync between AD domains

The PowerShell script given below synchronizes the passwords of users between their user accounts in two domains. ADSelfService Plus, a self-service password management and single sign-on solution, synchronizes changes made to a domain user's password to their user accounts in other Active Directory domains and even their user accounts in enterprise applications such as Google Workspace (formerly G Suite) and Office 365. Here is a comparison between password synchronization between two AD domains using PowerShell and ADSelfService Plus:

With PowerShell

In order to synchronize passwords across a user's account in multiple domains, the DS-Internals module needs to be installed

Install-Module -Name DSInternals

Once you have installed the DS-Internals module, run the following script, create your credentials with this script:

$credential = Get-Credential; $credential | Export-CliXml -Path '<enter the path of an XML file here>';

Now, run the following script

$sourceDomainNetBIOS = '<primary domain>';
$sourceDomainFQDN = '<primary domain>.com';
$sourceDomainDN = 'DC=<primary domain>,DC=com';
$sourceDomainCredential = Import-CliXml -Path '<enter the file path of an xml file here>';
$targetDomainNetBIOS = '<secondary domain>';
$targetDomainFQDN = '<secondary domain>.com';
$targetDomainDN = 'DC=<secondary domain>,DC=com';
$targetDomainCredential = Import-CliXml -Path '<enter file path of an XML file here>';
$syncGroup = 'Some Group'; $hashes = Get-ADReplAccount -All -NamingContext $sourceDomainDN -Server $sourceDomainFQDN -Credential $sourceDomainCredential;
$users = Get-ADGroupMember $syncGroup -server $targetDomainFQDN -Credential $targetDomainCredential;
foreach ($user in $users)
{
$currentUserHash = $hashes | ? {$_.saMAccountName -eq $user.SamAccountName};
$NTHash = ([System.BitConverter]::ToString($currentUserHash.NTHash) -replace '-','').ToLower();
Set-SamAccountPasswordHash -SamAccountName $user.SamAccountName -Domain $targetDomainNetBIOS -NTHash $NTHash -Server $targetDomainFQDN -Credential
$targetDomainCredential;
With ADSelfService Plus

The Password Synchronization feature synchronizes the changes made to a domain user's password with their user accounts in other domains and enterprise applications.

For configuration:

  • In ADSelfService Plus, go to Application, Click Active Directory.
  • Provide an Application Name
  • Select the Domain Name of the domain to which the passwords must be synced.
  • Select the ADSelfService Plus policy whose users' password must be synced with their user accounts in other domains.
  • Click Advanced and select the Source Attribute and the Target Attribute in the domain to be synced. When these two attributes are linked to the user accounts in both the domains, the passwords are synced from the primary domain to the secondary.
  • Click Add Application.

Advantages of ADSelfService Plus:

  • Specify which user can synchronize their AD domain passwords: While creating the ADSelfService Plus policy, users belonging to specific domains, OUs and groups can be enabled to synchronize their passwords.
  • Synchronize the passwords between multiple domains: Administrators can enable the synchronization of AD passwords between any number of domains with just a few clicks.
  • Synchronize password changes made through multiple mediums: With ADSelfService Plus, password resets made from the ADSelfService portal and mobile app, and the Windows, macOS, and Linux login screens can be synchronized with the integrated applications. Native password changes (password resets in the ADUC portal and password changes in the Ctrl+Alt+Del console) can also be synchronized.
  • Synchronize AD password across multiple applications:Using ADSelfService Plus, users' passwords can be synchronized between AD domains and other enterprise systems and applications like AD LDS, Office 365 and Salesforce.
  • Audit password synchronization: The synchronization actions during a password reset and change are audited as reports that can be easily generated with a single click and exported in formats like CSV, HTML, XLS, and PDF.

Sync passwords between AD domains using PowerShell

Step 1: Install and import the required modules

Ensure the Active Directory module is installed and loaded by running the script below. This allows PowerShell to interact with AD.

Import-Module ActiveDirectory

Step 2: Configure trust between AD domains

For password synchronization to work, ensure both domains trust each other. This establishes a trust relationship between domainA.com and another domain.

New-ADObject -Name "Trust" -Type container -Path "CN=System,DC=domainA,DC=com"

Step 3: Enable password replication

Allow password changes to replicate between domains. This enables password replication between domainA.com and domainB.com.

Set-ADReplicationAttribute -Source "domainA.com" -Target "domainB.com" -Attribute "unicodePwd"

Step 4: Sync passwords manually

Trigger an immediate password sync using the script below. This syncs only changed passwords instead of a full sync.

Start-ADSyncSyncCycle -PolicyType Delta

FAQs

1. How can I manually trigger password synchronization between AD domains?

Manually manually trigger password synchronization using the script below. This forces synchronization of changed passwords between domains.

Start-ADSyncSyncCycle -PolicyType Delta

2. How do I check if password sync is enabled?

Check if password sync is enabled by running the script below. If an AD connector exists, sync is enabled.

Get-ADSyncConnector | Select-Object Name, Type

3. Can I sync passwords between two different forests?

Yes, but Azure AD Connect must be set up for cross-forest sync.

Synchronize users Active Directory passwords across domains.

ADSelfService Plus trusted by

A single pane of glass for complete self service password management
Email Download Link