< Back

Rename-DbaDatabase

Mon Jan 13, 2020 1:28 pm

NAME Rename-DbaDatabase



SYNOPSIS

Changes database name, logical file names, file group names and physical file names (optionally handling the

move). BETA VERSION.





SYNTAX

Rename-DbaDatabase -SqlInstance <Sqlcollaborative.Dbatools.Parameter.DbaInstanceParameter[]> [-SqlCredential

<Pscredential>] [-Database <System.Object[]>] [-ExcludeDatabase <System.Object[]>] [-AllDatabases <Switch>]

[-DatabaseName <String>] [-FileGroupName <String>] [-LogicalName <String>] [-FileName <String>] [-ReplaceBefore

<Switch>] [-Force <Switch>] [-Move <Switch>] [-SetOffline <Switch>] [-Preview <Switch>] [-EnableException

<Switch>] [<CommonParameters>]



Rename-DbaDatabase [-SqlCredential <Pscredential>] [-ExcludeDatabase <System.Object[]>] [-AllDatabases <Switch>]

[-DatabaseName <String>] [-FileGroupName <String>] [-LogicalName <String>] [-FileName <String>] [-ReplaceBefore

<Switch>] [-Force <Switch>] [-Move <Switch>] [-SetOffline <Switch>] [-Preview <Switch>] -InputObject

<Microsoft.SqlServer.Management.Smo.Database[]> [-EnableException <Switch>] [<CommonParameters>]





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





PARAMETERS

-AllDatabases [<Switch>]

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



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-Database [<System.Object[]>]

Targets only specified databases



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-DatabaseName [<String>]

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

- <DBN> current database name

- <DATE> date (yyyyMMdd)



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-EnableException [<Switch>]

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.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-ExcludeDatabase [<System.Object[]>]

Excludes only specified databases



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-FileGroupName [<String>]

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

- <FGN> current filegroup name

- <DBN> current database name

- <DATE> date (yyyyMMdd)

If distinct names cannot be generated, a counter will be appended (0001, 0002, 0003, etc)



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-FileName [<String>]

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

- <FNN> current file name (the basename, without directory nor extension)

- <FT> file type (ROWS, LOG, MMO, FS)

- <LGN> current logical name

- <FGN> current filegroup name

- <DBN> current database name

- <DATE> date (yyyyMMdd)

If distinct names cannot be generated, a counter will be appended (0001, 0002, 0003, etc)



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-Force [<Switch>]

Kills any open session to be able to do renames.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-InputObject [<Microsoft.SqlServer.Management.Smo.Database[]>]

Accepts piped database objects



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-LogicalName [<String>]

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

- <FT> file type (ROWS, LOG)

- <LGN> current logical name

- <FGN> current filegroup name

- <DBN> current database name

- <DATE> date (yyyyMMdd)

If distinct names cannot be generated, a counter will be appended (0001, 0002, 0003, etc)



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-Move [<Switch>]

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



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-Preview [<Switch>]

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

parameters ;-) )



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-ReplaceBefore [<Switch>]

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 '<DBN><FNN>'

would end up with this logic:

- database --> no placeholders specified

- dbatools_HR to dbatools_HRARCHIVE

- filenames placeholders specified

<DBN><FNN> --> 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 '<DBN><FNN>'

-ReplaceBefore

end up with this logic instead:

- database --> no placeholders specified

- dbatools_HR to dbatools_HRARCHIVE

- filenames placeholders specified,

<DBN><FNN>, 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



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-SetOffline [<Switch>]

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



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-SqlCredential [<Pscredential>]

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.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-SqlInstance [<Sqlcollaborative.Dbatools.Parameter.DbaInstanceParameter[]>]

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



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



<CommonParameters>

This cmdlet supports the common parameters: Verbose, Debug,

ErrorAction, ErrorVariable, WarningAction, WarningVariable,

OutBuffer, PipelineVariable, and OutVariable. For more information, see

about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).



INPUTS



OUTPUTS



NOTES





Tags: Database, Rename

Author: Simone Bizzotto (@niphold)



Website: https://dbatools.io

Copyright: (c) 2018 by dbatools, licensed under MIT

License: MIT https://opensource.org/licenses/MIT



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



RELATED LINKS

https://dbatools.io/Rename-DbaDatabase