dbatools is a free PowerShell module with over 300 SQL Server administration, best practice and migration commands included.

Please note that documentation and command names may be out of date while we work furiously towards 1.0


Drop orphan database users with no matching SQL Server login. If a matching login exists, the drop will not be performed unless you specify the -Force parameter.


Remove all orphan users with no matching login from database ‘db5’
Try to remove orphan users that have matching login
Remove orphan users even the ones that have a matching login. Using -Force


To find and drop all orphan users without matching login of all databases present on server ‘sql2005’


To remove from all databases the user ‘OrphanUser’ only if not have their matching login


To find and drop all orphan users even if exists their matching login on both db1 and db2 databases


To remove from all databases the user OrphanUser EVEN if exists their matching login.


This command was created by Cláudio Silva. You can find Cláudio on Twitter and LinkedIn.


From PowerShell, execute Get-Help Remove-DbaOrphanUser -Detailed for more information on this function.

Source Code

Want to see the source code? View Remove-DbaOrphanUser.ps1 on GitHub

Related commands