Author | Simone Bizzotto (@niphold) |
Availability | Windows, Linux, macOS |
Want to see the source code for this command? Check out Rename-DbaDatabase on GitHub.
Want to see the Bill Of Health for this command? Check out Rename-DbaDatabase.
Changes database name, logical file names, file group names and physical file names (optionally handling the move). BETA VERSION.
Can change every database metadata that can be renamed.
The ultimate goal is choosing to have a default template to enforce in your environment
so your naming convention for every bit can be put in place in no time.
The process is as follows (it follows the hierarchy of the entities):
It returns an object for each database with all the renames done, plus hidden properties showing a "human" representation of them.
It's better you store the resulting object in a variable so you can inspect it in case of issues, e.g. "$result = Rename-DbaDatabase ....."
To get a grasp without worrying of what would happen under the hood, use "Rename-DbaDatabase .... -Preview | Select-Object *"
Rename-DbaDatabase -SqlInstance <DbaInstanceParameter[]>
[-SqlCredential <PSCredential>]
[-Database <Object[]>]
[-ExcludeDatabase <Object[]>]
[-AllDatabases]
[-DatabaseName <String>]
[-FileGroupName <String>]
[-LogicalName <String>]
[-FileName <String>]
[-ReplaceBefore]
[-Force]
[-Move]
[-SetOffline]
[-Preview]
[-EnableException]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
Rename-DbaDatabase
[-SqlCredential <PSCredential>]
[-ExcludeDatabase <Object[]>]
[-AllDatabases]
[-DatabaseName <String>]
[-FileGroupName <String>]
[-LogicalName <String>]
[-FileName <String>]
[-ReplaceBefore]
[-Force]
[-Move]
[-SetOffline]
[-Preview]
-InputObject <Database[]>
[-EnableException]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
PS C:\> Rename-DbaDatabase -SqlInstance sqlserver2014a -Database HR -DatabaseName HR2 -Preview | Select-Object *
Shows the detailed result set you'll get renaming the HR database to HR2 without doing anything
PS C:\> Rename-DbaDatabase -SqlInstance sqlserver2014a -Database HR -DatabaseName HR2
Renames the HR database to HR2
PS C:\> Get-DbaDatabase -SqlInstance sqlserver2014a -Database HR | Rename-DbaDatabase -DatabaseName HR2
Same as before, but with a piped database (renames the HR database to HR2)
PS C:\> Rename-DbaDatabase -SqlInstance sqlserver2014a -Database HR -DatabaseName "dbatools_<DBN>"
Renames the HR database to dbatools_HR
PS C:\> Rename-DbaDatabase -SqlInstance sqlserver2014a -Database HR -DatabaseName "dbatools_<DBN>_<DATE>"
Renames the HR database to dbatools_HR_20170807 (if today is 07th Aug 2017)
PS C:\> Rename-DbaDatabase -SqlInstance sqlserver2014a -Database HR -FileGroupName "dbatools_<FGN>"
Renames every FileGroup within HR to "dbatools_[the original FileGroup name]"
PS C:\> Rename-DbaDatabase -SqlInstance sqlserver2014a -Database HR -DatabaseName "dbatools_<DBN>" -FileGroupName "<DBN>_<FGN>"
Renames the HR database to "dbatools_HR", then renames every FileGroup within to "dbatools_HR_[the original FileGroup name]"
PS C:\> Rename-DbaDatabase -SqlInstance sqlserver2014a -Database HR -FileGroupName "dbatools_<DBN>_<FGN>"
PS C:\> Rename-DbaDatabase -SqlInstance sqlserver2014a -Database HR -DatabaseName "dbatools_<DBN>"
Renames the HR database to "dbatools_HR", then renames every FileGroup within to "dbatools_HR_[the original FileGroup name]"
PS C:\> Rename-DbaDatabase -SqlInstance sqlserver2014a -Database HR -DatabaseName "dbatools_<DBN>" -FileName "<DBN>_<FGN>_<FNN>"
Renames the HR database to "dbatools_HR" and then all filenames as "dbatools_HR_[Name of the FileGroup]_[original_filename]"
The db stays online (watch out!). You can then proceed manually to move/copy files by hand, set the db offline and then online again to finish the rename process
PS C:\> Rename-DbaDatabase -SqlInstance sqlserver2014a -Database HR -DatabaseName "dbatools_<DBN>" -FileName "<DBN>_<FGN>_<FNN>" -SetOffline
Renames the HR database to "dbatools_HR" and then all filenames as "dbatools_HR_[Name of the FileGroup]_[original_filename]"
The db is then set offline (watch out!). You can then proceed manually to move/copy files by hand and then set it online again to finish the rename process
PS C:\> Rename-DbaDatabase -SqlInstance sqlserver2014a -Database HR -DatabaseName "dbatools_<DBN>" -FileName "<DBN>_<FGN>_<FNN>" -Move
Renames the HR database to "dbatools_HR" and then all filenames as "dbatools_HR_[Name of the FileGroup]_[original_filename]"
The db is then set offline (watch out!). The function tries to do a simple rename and then sets the db online again to finish the rename process
Target any number of instances, in order to return their build state.
Alias | |
Required | True |
Pipeline | false |
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 |
Targets only specified databases
Alias | |
Required | False |
Pipeline | false |
Default Value |
Excludes only specified databases
Alias | |
Required | False |
Pipeline | false |
Default Value |
If you want to apply the naming convention system wide, you need to pass this parameter
Alias | |
Required | False |
Pipeline | false |
Default Value | False |
Pass a template to rename the database name. Valid placeholders are:
Alias | |
Required | False |
Pipeline | false |
Default Value |
Pass a template to rename file group name. Valid placeholders are:
Alias | |
Required | False |
Pipeline | false |
Default Value |
Pass a template to rename logical name. Valid placeholders are:
Alias | |
Required | False |
Pipeline | false |
Default Value |
Pass a template to rename file name. Valid placeholders are:
Alias | |
Required | False |
Pipeline | false |
Default Value |
If you pass this switch, all upper level "current names" will be inspected and replaced BEFORE doing the
rename according to the template in the current level (remember the hierarchy):
Let's say you have a database named "dbatools_HR", composed by 3 files
Alias | |
Required | False |
Pipeline | false |
Default Value | False |
Kills any open session to be able to do renames.
Alias | |
Required | False |
Pipeline | false |
Default Value | False |
If you want this function to move files, else you're the one in charge of it.
This enables the same functionality as SetOffline, killing open transactions and putting the database
offline, then do the actual rename and setting it online again afterwards
Alias | |
Required | False |
Pipeline | false |
Default Value | False |
Kills any open session and sets the database offline to be able to move files
Alias | |
Required | False |
Pipeline | false |
Default Value | False |
Shows the renames without performing any operation (recommended to find your way around this function parameters ;-) )
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 |
If this switch is enabled, no actions are performed but informational messages will be displayed that explain what would happen if the command were to run.
Alias | wi |
Required | False |
Pipeline | false |
Default Value |
If this switch is enabled, you will be prompted for confirmation before executing any operations that change state.
Alias | cf |
Required | False |
Pipeline | false |
Default Value |