Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Kevin-Bronsdijk/754e70d1e9bebb11adc9 to your computer and use it in GitHub Desktop.
Save Kevin-Bronsdijk/754e70d1e9bebb11adc9 to your computer and use it in GitHub Desktop.
sql-server-emergency-maintenance
Import-Module SQLPS -DisableNameChecking
#replace this with your instance name
$instanceName = "server\instance";
$srv = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $instanceName;
$dbName = "DatabaseName"
$db = $srv.Databases.Item($dbName)
# Stops the all processes on the specified database.
$server.KillAllprocesses($dbName);
# Alter the files in SQL server (new path and/or name)
$db.FileGroups["NameFileGroup"].Files['LogicalName'].FileName = 'path\filename'
# Cannot move data files while online
$db.SetOffline();
$db.Alter();
# Move
Move-Item 'path\filename' 'path'
# And back online again
$db.SetOnline();
$db.Alter();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment