Execution Flow
Intentionally NOT designed for unattended use. A missing AD account may be caused by a transient DC or trust issue — not a real orphan. Always run with
-WhatIf first to audit what would be removed before committing.A rollback SQL script (
CREATE LOGIN FROM WINDOWS + ALTER SERVER ROLE statements) is written to -BackupPath before each removal. The script is usable only if the AD account is later re-created.Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
| -SqlInstance | String | Optional | Target SQL instance. Default: $env:COMPUTERNAME. |
| -SqlCredential | PSCredential | Optional | SQL authentication credential. |
| -ExcludeLogin | String[] | Optional | Additional logins to protect (wildcards supported). |
| -AdModuleAction | String | Optional | Abort (default) / Install — behavior when the AD module is missing. Skip is treated as Abort for this destructive operation. |
| -BackupPath | String | Optional | Directory for rollback scripts. Default: C:\System\WinSrvLog\MSSQL. |
| -SkipBackup | Switch | Optional | Skip writing rollback scripts. Not recommended. |
| -EnableException | Switch | Optional | Throws terminating errors instead of returning an error status object. |
| -WhatIf / -Confirm | Switch | Optional | Standard ShouldProcess support (ConfirmImpact=High). |
Return Value
Returns an array of PSCustomObject, one per login evaluated, each with: SqlInstance, Login, Status (Removed / Skipped / WhatIf / Failed), Reason, BackupFile, Timestamp.
Examples
Example 1 — Audit what would be removed (safe)
Remove-sqmAdOrphanLogin -SqlInstance "SQL01" -WhatIf
Example 2 — Remove confirmed orphans with exclusion
Remove-sqmAdOrphanLogin -SqlInstance "SQL01" -ExcludeLogin 'DOMAIN\KeepThis*'