commands

^

Rename-DbaDatabase

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.

Synopsis

Changes database name, logical file names, file group names and physical file names (optionally handling the move). BETA VERSION.

Description

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

Syntax

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>]

 

Examples

 

Example: 1
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

Example: 2
PS C:\> Rename-DbaDatabase -SqlInstance sqlserver2014a -Database HR -DatabaseName HR2

Renames the HR database to HR2

Example: 3
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)

Example: 4
PS C:\> Rename-DbaDatabase -SqlInstance sqlserver2014a -Database HR -DatabaseName "dbatools_<DBN>"

Renames the HR database to dbatools_HR

Example: 5
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)

Example: 6
PS C:\> Rename-DbaDatabase -SqlInstance sqlserver2014a -Database HR -FileGroupName "dbatools_<FGN>"

Renames every FileGroup within HR to "dbatools_[the original FileGroup name]"

Example: 7
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]"

Example: 8
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]"

Example: 9
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

Example: 10
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

Example: 11
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

Required Parameters

-SqlInstance

Target any number of instances, in order to return their build state.

Alias
Required True
Pipeline false
Default Value
-InputObject

Accepts piped database objects

Alias
Required True
Pipeline true (ByValue)
Default Value

Optional Parameters

-SqlCredential

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
-Database

Targets only specified databases

Alias
Required False
Pipeline false
Default Value
-ExcludeDatabase

Excludes only specified databases

Alias
Required False
Pipeline false
Default Value
-AllDatabases

If you want to apply the naming convention system wide, you need to pass this parameter

Alias
Required False
Pipeline false
Default Value False
-DatabaseName

Pass a template to rename the database name. Valid placeholders are:

  • current database name
  • date (yyyyMMdd)
Alias
Required False
Pipeline false
Default Value
-FileGroupName

Pass a template to rename file group name. Valid placeholders are:

  • current filegroup name
  • current database name
  • date (yyyyMMdd) If distinct names cannot be generated, a counter will be appended (0001, 0002, 0003, etc)
Alias
Required False
Pipeline false
Default Value
-LogicalName

Pass a template to rename logical name. Valid placeholders are:

  • file type (ROWS, LOG)
  • current logical name
  • current filegroup name
  • current database name
  • date (yyyyMMdd) If distinct names cannot be generated, a counter will be appended (0001, 0002, 0003, etc)
Alias
Required False
Pipeline false
Default Value
-FileName

Pass a template to rename file name. Valid placeholders are:

  • current file name (the basename, without directory nor extension)
  • file type (ROWS, LOG, MMO, FS)
  • current logical name
  • current filegroup name
  • current database name
  • date (yyyyMMdd) If distinct names cannot be generated, a counter will be appended (0001, 0002, 0003, etc)
Alias
Required False
Pipeline false
Default Value
-ReplaceBefore

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

  • dbatools_HR_Data.mdf
  • dbatools_HR_Index.ndf
  • dbatools_HR_log.ldf Rename-DbaDatabase .... -Database "dbatools_HR" -DatabaseName "dbatools_HRARCHIVE" -FileName '' would end up with this logic:
  • database --> no placeholders specified
  • dbatools_HR to dbatools_HRARCHIVE
  • filenames placeholders specified --> current database name + current filename"
  • dbatools_HR_Data.mdf to dbatools_HRARCHIVEdbatools_HR_Data.mdf
  • dbatools_HR_Index.mdf to dbatools_HRARCHIVEdbatools_HR_Data.mdf
  • dbatools_HR_log.ldf to dbatools_HRARCHIVEdbatools_HR_log.ldf Passing this switch, instead, e.g. Rename-DbaDatabase .... -Database "dbatools_HR" -DatabaseName "dbatools_HRARCHIVE" -FileName '' -ReplaceBefore end up with this logic instead:
  • database --> no placeholders specified
  • dbatools_HR to dbatools_HRARCHIVE
  • filenames placeholders specified, , plus -ReplaceBefore --> current database name + replace OLD "upper level" names inside the current filename
  • dbatools_HR_Data.mdf to dbatools_HRARCHIVE_Data.mdf
  • dbatools_HR_Index.mdf to dbatools_HRARCHIVE_Data.mdf
  • dbatools_HR_log.ldf to dbatools_HRARCHIVE_log.ldf
Alias
Required False
Pipeline false
Default Value False
-Force

Kills any open session to be able to do renames.

Alias
Required False
Pipeline false
Default Value False
-Move

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
-SetOffline

Kills any open session and sets the database offline to be able to move files

Alias
Required False
Pipeline false
Default Value False
-Preview

Shows the renames without performing any operation (recommended to find your way around this function parameters ;-) )

Alias
Required False
Pipeline false
Default Value False
-EnableException

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
-WhatIf

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
-Confirm

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