dbatools is a free PowerShell module with over 200 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


Remove-DbaDbUser

If user is the owner of a schema with the same name and if the schema does not have any underlying objects the schema will be dropped. If user owns more than one schema, the owner of the schemas that does not have the same name as the user, will be changed to ‘dbo’. If schemas have underlying objects, you must specify the -Force parameter so the user can be dropped.

Screenshots

dbatools-Remove-DbaDbUser

Examples

Drops user1 from all databases it exists in on server ‘sqlserver2014’.

Remove-DbaDbUser -SqlInstance sqlserver2014 -User user1

Drops user1 from the database1 database on server ‘sqlserver2014’.

Remove-DbaDbUser -SqlInstance sqlserver2014 -Database database1 -User user1

Drops user1 from all databases it exists in on server ‘sqlserver2014’ except for the model database.

Remove-DbaDbUser -SqlInstance sqlserver2014 -ExcludeDatabase model -User user1

Drops user1 from all databases it exists in on server ‘sqlserver2014’.

Get-DbaDatabaseUser sqlserver2014 | Where-Object Name -In “user1” | Remove-DbaDbUser

Author

This command was created by Doug Meyers. You can find Doug on Twitter.

Get-Help

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

Source Code

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

Related commands