Author | Claudio Silva (@ClaudioESSilva) , Simone Bizzotto (@niphlod) |
Availability | Windows, Linux, macOS |
Want to see the source code for this command? Check out Remove-DbaDbOrphanUser on GitHub.
Want to see the Bill Of Health for this command? Check out Remove-DbaDbOrphanUser.
Removes orphaned database users that no longer have corresponding SQL Server logins
Removes orphaned database users from one or more databases, handling schema ownership transfers automatically to prevent dependency issues.
Orphaned users occur when a database user exists but its corresponding login in the master database has been deleted or doesn't exist on the current server. This commonly happens after login deletions, database migrations, or restores to servers where the original logins don't exist.
The function intelligently handles schema ownership:
When a login with the same name exists on the server (suggesting the user could be repaired with Repair-DbaDbOrphanUser instead), removal is blocked unless -Force is specified. This safety check prevents accidental deletions when remediation might be more appropriate than removal.
Contained databases are automatically skipped since they manage authentication differently and cannot have orphaned users in the traditional sense.
Remove-DbaDbOrphanUser
[-SqlInstance] <DbaInstanceParameter[]>
[[-SqlCredential] <PSCredential>]
[[-Database] <Object[]>]
[[-ExcludeDatabase] <Object[]>]
[[-User] <Object[]>]
[-Force]
[-EnableException]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
PS C:\> Remove-DbaDbOrphanUser -SqlInstance sql2005
Finds and drops all orphan users without matching Logins in all databases present on server 'sql2005'.
PS C:\> Remove-DbaDbOrphanUser -SqlInstance sqlserver2014a -SqlCredential $cred
Finds and drops all orphan users without matching Logins in all databases present on server 'sqlserver2014a'. SQL Server authentication will be used in connecting to the server.
PS C:\> Remove-DbaDbOrphanUser -SqlInstance sqlserver2014a -Database db1, db2 -Force
Finds and drops orphan users even if they have a matching Login on both db1 and db2 databases.
PS C:\> Remove-DbaDbOrphanUser -SqlInstance sqlserver2014a -ExcludeDatabase db1, db2 -Force
Finds and drops orphan users even if they have a matching Login from all databases except db1 and db2.
PS C:\> Remove-DbaDbOrphanUser -SqlInstance sqlserver2014a -User OrphanUser
Removes user OrphanUser from all databases only if there is no matching login.
PS C:\> Remove-DbaDbOrphanUser -SqlInstance sqlserver2014a -User OrphanUser -Force
Removes user OrphanUser from all databases even if they have a matching Login. Any schema that the user owns will change ownership to dbo.
The target SQL Server instance or instances.
Alias | |
Required | True |
Pipeline | true (ByValue) |
Default Value |
Login to the target instance using alternative credentials. Accepts PowerShell credentials (Get-Credential).
Windows Authentication, SQL Server Authentication, Active Directory - Password, and Active Directory - Integrated are all supported.
For MFA support, please use Connect-DbaInstance.
Alias | |
Required | False |
Pipeline | false |
Default Value |
Specifies which databases to check for orphaned users. Accepts single database names, comma-separated lists, or arrays.
When omitted, all accessible, non-read-only databases on the instance are processed. Contained databases are automatically skipped since they cannot have orphaned users.
Alias | |
Required | False |
Pipeline | false |
Default Value |
Specifies databases to skip during orphaned user removal. Useful when you want to process most databases but avoid specific ones.
Commonly used to exclude system databases, databases undergoing maintenance, or databases where user cleanup should be handled separately.
Alias | |
Required | False |
Pipeline | false |
Default Value |
Specifies specific orphaned users to target for removal instead of processing all orphaned users found.
Use this when you need to remove only certain orphaned users rather than all orphans in the database. The function will verify these users are actually orphaned before removal.
Alias | |
Required | False |
Pipeline | true (ByValue) |
Default Value |
Bypasses safety checks that normally prevent orphaned user removal in potentially problematic scenarios.
Required when the user owns schemas containing objects (ownership transfers to 'dbo') or when a matching login exists on the server (suggesting repair might be more appropriate than removal).
Use with caution as this can change schema ownership and remove users that could potentially be repaired instead.
Alias | |
Required | False |
Pipeline | false |
Default Value | False |
By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting.
Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.
Alias | |
Required | False |
Pipeline | false |
Default Value | False |
If this switch is enabled, no actions are performed but informational messages will be displayed that explain what would happen if the command were to run.
Alias | wi |
Required | False |
Pipeline | false |
Default Value |
If this switch is enabled, you will be prompted for confirmation before executing any operations that change state.
Alias | cf |
Required | False |
Pipeline | false |
Default Value |