Skip to content

Instantly share code, notes, and snippets.

@ElliotWood
Created March 28, 2013 02:32
Show Gist options
  • Save ElliotWood/5260063 to your computer and use it in GitHub Desktop.
Save ElliotWood/5260063 to your computer and use it in GitHub Desktop.
Rename-SQLDatabase powershell
Function Rename-SQLDatabase {
param (
[string] $ServerName,
[string] $SourceDb,
[string] $DestDb
)
$connection = New-Object System.Data.SqlClient.SqlConnection
$command = New-Object System.Data.SqlClient.SqlCommand
$connection.ConnectionString = "Server=$ServerName;Integrated Security=True;"
$command.CommandText = "ALTER DATABASE [$SourceDb] SET OFFLINE WITH ROLLBACK IMMEDIATE;ALTER DATABASE [$SourceDb] SET ONLINE;EXEC sp_renamedb [$SourceDb], [$DestDb];"
$command.Connection = $connection
$command.Connection.Open();
$command.ExecuteNonQuery();
$command.Connection.Close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment