Commands / Set-sqmDatabaseOwner
Set-sqmDatabaseOwner
ConfigurationsqmSQLTool v1.8.2+ · Database Admin⚠ Alters Ownership  ✓ Validation
Changes the database owner (dbo schema owner) to a specified principal with validation of the new owner's existence and permissions. Supports both SQL logins and Windows AD accounts. Handles orphaned database users and validates that the new owner has appropriate roles before changing ownership.

Parameters

ParameterTypeRequiredDefaultNotes
-SqlInstancestringRequiredTarget SQL Server instance.
-SqlCredentialPSCredentialOptionalCredentials for the connection.
-Databasestring[]RequiredTarget databases (name or wildcard pattern).
-NewOwnerstringRequiredNew owner principal name (SQL login or Windows account).
-ContinueOnErrorswitchSwitch$falseContinue to next database on error.
-EnableExceptionswitchSwitch$falseRe-throw exceptions immediately.
-WhatIf / -ConfirmswitchOptionalShouldProcess (ConfirmImpact: Medium) guards ownership change.

Execution Flow

START dbatools available? NO throw: dbatools not found YES Connect to instance with sysadmin credentials Verify instance is online Validate -NewOwner exists at instance level Get-DbaLogin: verify SQL login or Windows principal If not found: throw error and abort foreach $db in $databases Get current database owner Query dbo.owner or Get-DbaDatabase Owner property Already new owner? NO Skip: already new owner YES User exists in DB? NO CREATE USER from login YES ShouldProcess? (ConfirmImpact: Medium) NO WhatIf: show change plan YES ALTER AUTHORIZATION ON DATABASE [db] TO -NewOwner Change dbo ownership Return [PSCustomObject[]] per database Database, OldOwner, NewOwner, UserCreated, Status DONE

Examples

Change single database owner
Set-sqmDatabaseOwner -SqlInstance "SQL01" -Database "MyDB" -NewOwner "domain\admin"
Change owner for multiple databases with -WhatIf
Set-sqmDatabaseOwner -SqlInstance "SQL01" -Database "Prod*" -NewOwner "sqlsvc" -WhatIf