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


Rename-DbaDatabase

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):
– database name is changed (optionally, forcing users out)
– filegroup name(s) are changed accordingly
– logical name(s) are changed accordingly
– physical file(s) are changed accordingly
– if Move is specified, the database will be taken offline and the move will initiate, then it will be taken online
– if Move is not specified, the database remains online (unless SetOffline), and you are in charge of moving files

If any of the above fails, the process stops.
Please take a backup of your databases BEFORE using this, and remember to backup AFTER (also a FULL backup of master)

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 *”

Screenshots

dbatools-Rename-DbaDatabase

Examples

Shows the detailed result set you’ll get renaming the HR database to HR2 without doing anything.

Rename-DbaDatabase -SqlInstance sqlserver2014a -Database HR -DatabaseName HR2 | select *

Renames the HR database to HR2.

Rename-DbaDatabase -SqlInstance sqlserver2014a -Database HR -DatabaseName HR2

Same as before, but with a piped database (renames the HR database to HR2).

Get-DbaDatabase -SqlInstance sqlserver2014a -Database HR | Rename-DbaDatabase -DatabaseName HR2

Renames the HR database to dbatools_HR.

Rename-DbaDatabase -SqlInstance sqlserver2014a -Database HR -DatabaseName “dbatools_

Renames the HR database to dbatools_HR_20170807 (if today is 07th Aug 2017).

Rename-DbaDatabase -SqlInstance sqlserver2014a -Database HR -DatabaseName “dbatools__

Renames every FileGroup within HR to “dbatools_[the original FileGroup name]”.

Rename-DbaDatabase -SqlInstance sqlserver2014a -Database HR -FileGroupName “dbatools_

Renames the HR database to “dbatools_HR”, then renames every FileGroup within to “dbatools_HR_[the original FileGroup name]”.
Note the “default recursive behavior” here: for all intents and purposes the result of the former can be obtained with two distinct calls:
Rename-DbaDatabase -SqlInstance sqlserver2014a -Database HR -FileGroupName “dbatools__
Rename-DbaDatabase -SqlInstance sqlserver2014a -Database HR -DatabaseName “dbatools_

Rename-DbaDatabase -SqlInstance sqlserver2014a -Database HR -DatabaseName “dbatools_” -FileGroupName “_

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.

Rename-DbaDatabase -SqlInstance sqlserver2014a -Database HR -DatabaseName “dbatools_” -FileName “__

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.

Rename-DbaDatabase -SqlInstance sqlserver2014a -Database HR -DatabaseName “dbatools_” -FileName “__” -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!). The function tries to do a simple rename and then sets the db online again to finish the rename process.

Rename-DbaDatabase -SqlInstance sqlserver2014a -Database HR -DatabaseName “dbatools_” -FileName “__” -Move

Author

This command was created by Simone Bizzotto. You can find Simone on Twitter.

Get-Help

From PowerShell, execute Get-Help Rename-DbaDatabase -Detailed for more information on this function.

Source Code

Want to see the source code? View Rename-DbaDatabase.ps1 on GitHub
 

Related commands