Why not just primary to secondary?
-SyncMode Union reads all replicas and fills the gaps in every direction. The previous behaviour is still available as -SyncMode Primary.Execution Flow (SyncMode Union)
master.dbo.sqmLoginSyncState. A login that was on a replica before and is missing now was dropped there on purpose, so it is not recreated from another replica. Drop it on the other replicas too, or distribute it again with -ResetTombstone. First run: secondaries receive missing logins as before; logins missing on the primary are only reported, because the tool cannot know whether they were deleted there deliberately in the past. Check the report after the first run.ALTER LOGIN ... WITH PASSWORD = <hash> HASHED, never DROP. A change is detected by PasswordLastSetTime rising on a replica since the last run (stored in the snapshot, in UTC), not by comparing hashes: SQL Server 2025 stores passwords in a new format (0x0300) and rewrites an older 0x0200 hash at the next login without touching PasswordLastSetTime, so the same password has different hashes on 2022 and 2025. A 0x0300 hash is never written to a replica older than SQL Server 2025; that case is reported. CHECK_POLICY and CHECK_EXPIRATION are switched off for the change (HASHED requires it) and restored afterwards. Two competing changes less than 60 seconds apart are only reported. A rollback script with the old hashes is written to -BackupPath first. Default database, language, disabled state and different SIDs are only reported.sa (SID 0x01, also when renamed), ##MS_* certificate logins and local machine accounts (HOST\user of any replica). Unless -IncludeSystemLogins is set: sysadmin members, NT SERVICE\*, NT AUTHORITY\* and BUILTIN\*.Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
| -SqlInstance | String | Optional | An instance of the AG, normally the primary. Default: $env:COMPUTERNAME. |
| -AvailabilityGroupName | String | Optional | AG to sync. If omitted, the first AG on the instance is used (with a warning if there are several). |
| -SyncMode | String | Optional | Union (default): missing logins in every direction, tombstones and conflicts only reported. Primary: previous behaviour, primary to secondaries, -Force overwrites existing logins. |
| -HashChange | String | Optional | Union only. Newest (default): a password changed on one replica is applied to the others. Report: only report the difference. |
| -ResetTombstone | String[] | Optional | Union only. Exact login names whose tombstone is removed on all replicas before the sync, so they are distributed again. |
| -SqlCredential | PSCredential | Optional | Credential for all replicas. Union mode uses one credential for all replicas (this one, otherwise -SourceCredential). |
| -SourceCredential | PSCredential | Optional | Primary mode: credential for the primary. |
| -DestinationCredential | PSCredential | Optional | Primary mode: credential for the secondaries. |
| -Login | String[] | Optional | Only these logins (wildcards allowed). |
| -ExcludeLogin | String[] | Optional | Logins to skip (wildcards allowed). |
| -IncludeSystemLogins | Switch | Optional | Also include sysadmin members and NT SERVICE\*, NT AUTHORITY\*, BUILTIN\*. sa and ##MS_* are never copied. |
| -SkipSecondaryServers | String[] | Optional | Secondary replicas to leave out (maintenance). |
| -AdjustAuthMode | Switch | Optional | Adjust the authentication mode of a target replica to match the source if needed. |
| -RestartServiceIfRequired | Switch | Optional | Restart the SQL Server service on a target replica if its authentication mode was changed. |
| -DisablePolicy | Switch | Optional | Disable the configured policy on the target during the copy (default: $true). |
| -AdModuleAction | String | Optional | Install, Skip or Abort if the ActiveDirectory module is missing. Not set: Install interactively, Skip in a SQL Agent job. |
| -AuditAdOrphans | Switch | Optional | After the sync, report Windows logins whose AD account no longer exists (event 9003). Detection only. |
| -Force | Switch | Optional | Primary mode only, ignored in Union mode. Overwrites existing logins on the secondaries (DROP + CREATE). Default $true in Primary mode. |
| -ForceIncludeOnly | String[] | Optional | Primary mode with -Force: only update these logins. |
| -ForceExclude | String[] | Optional | Primary mode with -Force: additional logins to leave untouched. |
| -SafeForceMode | Bool | Optional | Primary mode with -Force: exclude sysadmins, the Agent account and system logins (default $true). |
| -BackupLogins | Switch | Optional | Primary mode with -Force: script the existing logins of each secondary to a .sql file first. |
| -BackupPath | String | Optional | Folder for login backups and password rollback scripts. Default: configured output path. |
| -BackupRetentionDays | Int | Optional | Delete login backups (and audit reports) older than this many days. Default 7, 0 keeps all. |
| -NoReport | Switch | Optional | Do not write backup files (job context). |
| -EnableException | Switch | Optional | Throw instead of returning a Failed row. |
| -WhatIf / -Confirm | Switch | Optional | Standard ShouldProcess support. With -WhatIf nothing is copied and no snapshot is written. |
Return Value
Union: one PSCustomObject per action with AvailabilityGroup, SyncMode, Action (Copy, PasswordSync, Tombstone, Conflict, ReadReplica, ResetTombstone, UpdateState), LoginName, SourceReplica, TargetReplica, Status (Success, Warning, Failed, Skipped, WhatIf), Message, Timestamp. Rows with status Warning need a decision and are also written to the Application log (source sqmSQLTool, event 9004); Failed rows raise event 9002.
Primary: one object per secondary with PrimaryReplica, SecondaryReplica, Status, LoginsCount, OrphansRepaired, BackupFile, Error.
Examples
Example 1, Sync all replicas of an AG
Sync-sqmLoginsToAlwaysOn -SqlInstance "SQL01" -AvailabilityGroupName "AG_PROD"Example 2, Preview: what would be copied and reported
Sync-sqmLoginsToAlwaysOn -SqlInstance "SQL01" -AvailabilityGroupName "AG_PROD" -WhatIf |
Where-Object Status -ne 'Success' | Format-Table Action, LoginName, TargetReplica, Status, MessageExample 3, Distribute a reported login again
Sync-sqmLoginsToAlwaysOn -SqlInstance "SQL01" -AvailabilityGroupName "AG_PROD" -ResetTombstone "svc_app"Example 4, Report password differences instead of applying them
Sync-sqmLoginsToAlwaysOn -SqlInstance "SQL01" -AvailabilityGroupName "AG_PROD" -HashChange ReportExample 5, Only application logins, with AD orphan audit
Sync-sqmLoginsToAlwaysOn -SqlInstance "SQL01" -Login "DOMAIN\App*","svc_app" -AuditAdOrphansExample 6, Previous behaviour: primary to secondaries, new logins only
Sync-sqmLoginsToAlwaysOn -SqlInstance "SQL01" -SyncMode Primary -Force:$false-Name AgLoginSync on every replica. It runs daily and right after a failover (Agent alert on message 1480).