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
-SqlInstancestringOptional$env:COMPUTERNAMETarget SQL Server instance.
-SqlCredentialPSCredentialOptional, Credentials for the connection.
-Databasestring[]Optional@()Target databases (name or wildcard pattern).
-ContinueOnErrorswitchSwitch$falseContinue to next database on error.
-EnableExceptionswitchSwitch$falseRe-throw exceptions immediately.
-WhatIf / -ConfirmswitchOptional, ShouldProcess (ConfirmImpact: Medium) guards ownership change.
-ExcludeDatabasestring[]Optional@()Databases to exclude. Wildcards allowed.
-ForceswitchSwitch$falseAlso process databases that already have the correct owner (forces re-assignment).
-IncludeSystemDatabasesswitchSwitch$falseAlso include system databases (master, model, msdb). Default: $false. tempdb is always excluded.
-OutputPathstringOptional, Directory for the change log. Default: from module configuration.
-OwnerLoginstringOptional, Login to set as the new owner. Default: sa account (automatically determined via SID 0x01, regardless of whether it has been renamed).

Execution Flow

START dbatools available? NO throw: dbatools not found YES Connect to instance with sysadmin credentials Verify instance is online Validate -OwnerLogin 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 -OwnerLogin Change dbo ownership Return [PSCustomObject[]] per database Database, OldOwner, NewOwner, UserCreated, Status DONE

Examples

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