Author | Simone Bizzotto (@niphold) |
Availability | Windows, Linux, macOS |
Want to see the source code for this command? Check out Set-DbaDbState on GitHub.
Want to see the Bill Of Health for this command? Check out Set-DbaDbState.
Sets various options for databases, hereby called "states"
Sets some common "states" on databases:
Returns an object with SqlInstance, Database, RW, Status, Access, Notes
Notes gets filled when something went wrong setting the state
Set-DbaDbState
[-SqlCredential <PSCredential>]
[-Database <Object[]>]
[-ExcludeDatabase <Object[]>]
[-AllDatabases]
[-ReadOnly]
[-ReadWrite]
[-Online]
[-Offline]
[-Emergency]
[-Detached]
[-SingleUser]
[-RestrictedUser]
[-MultiUser]
[-Force]
[-EnableException]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
Set-DbaDbState -SqlInstance <DbaInstanceParameter[]>
[-SqlCredential <PSCredential>]
[-Database <Object[]>]
[-ExcludeDatabase <Object[]>]
[-AllDatabases]
[-ReadOnly]
[-ReadWrite]
[-Online]
[-Offline]
[-Emergency]
[-Detached]
[-SingleUser]
[-RestrictedUser]
[-MultiUser]
[-Force]
[-EnableException]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
Set-DbaDbState
[-SqlCredential <PSCredential>]
[-Database <Object[]>]
[-ExcludeDatabase <Object[]>]
[-AllDatabases]
[-ReadOnly]
[-ReadWrite]
[-Online]
[-Offline]
[-Emergency]
[-Detached]
[-SingleUser]
[-RestrictedUser]
[-MultiUser]
[-Force]
[-EnableException]
-InputObject <PSObject[]>
[-WhatIf]
[-Confirm]
[<CommonParameters>]
PS C:\> Set-DbaDbState -SqlInstance sqlserver2014a -Database HR -Offline
Sets the HR database as OFFLINE
PS C:\> Set-DbaDbState -SqlInstance sqlserver2014a -AllDatabases -Exclude HR -ReadOnly -Force
Sets all databases of the sqlserver2014a instance, except for HR, as READ_ONLY
PS C:\> Get-DbaDbState -SqlInstance sql2016 | Where-Object Status -eq 'Offline' | Set-DbaDbState -Online
Finds all offline databases and sets them to online
PS C:\> Set-DbaDbState -SqlInstance sqlserver2014a -Database HR -SingleUser
Sets the HR database as SINGLE_USER
PS C:\> Set-DbaDbState -SqlInstance sqlserver2014a -Database HR -SingleUser -Force
Sets the HR database as SINGLE_USER, dropping all other connections (and rolling back open transactions)
PS C:\> Get-DbaDatabase -SqlInstance sqlserver2014a -Database HR | Set-DbaDbState -SingleUser -Force
Gets the databases from Get-DbaDatabase, and sets them as SINGLE_USER, dropping all other connections (and rolling back open transactions)
The target SQL Server instance or instances
Alias | |
Required | True |
Pipeline | true (ByPropertyName) |
Default Value |
Accepts piped database objects
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 |
The database(s) to process - this list is auto-populated from the server. if unspecified, all databases will be processed.
Alias | |
Required | False |
Pipeline | false |
Default Value |
The database(s) to exclude - this list is auto-populated from the server
Alias | |
Required | False |
Pipeline | false |
Default Value |
This is a parameter that was included for safety, so you don't accidentally set options on all databases without specifying
Alias | |
Required | False |
Pipeline | false |
Default Value | False |
RW Option : Sets the database as READ_ONLY
Alias | |
Required | False |
Pipeline | false |
Default Value | False |
RW Option : Sets the database as READ_WRITE
Alias | |
Required | False |
Pipeline | false |
Default Value | False |
Status Option : Sets the database as ONLINE
Alias | |
Required | False |
Pipeline | false |
Default Value | False |
Status Option : Sets the database as OFFLINE
Alias | |
Required | False |
Pipeline | false |
Default Value | False |
Status Option : Sets the database as EMERGENCY
Alias | |
Required | False |
Pipeline | false |
Default Value | False |
Status Option : Detaches the database
Alias | |
Required | False |
Pipeline | false |
Default Value | False |
Access Option : Sets the database as SINGLE_USER
Alias | |
Required | False |
Pipeline | false |
Default Value | False |
Access Option : Sets the database as RESTRICTED_USER
Alias | |
Required | False |
Pipeline | false |
Default Value | False |
Access Option : Sets the database as MULTI_USER
Alias | |
Required | False |
Pipeline | false |
Default Value | False |
For most options, this translates to instantly rolling back any open transactions
that may be stopping the process.
For -Detached it is required to break mirroring and Availability Groups
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 |
Shows what would happen if the command were to run. No actions are actually performed.
Alias | wi |
Required | False |
Pipeline | false |
Default Value |
Prompts you for confirmation before executing any changing operations within the command.
Alias | cf |
Required | False |
Pipeline | false |
Default Value |