Skip to content

Instantly share code, notes, and snippets.

@PCfromDCSnippets
Created January 29, 2016 02:23
Show Gist options
  • Save PCfromDCSnippets/dcfeabb856e1cc576250 to your computer and use it in GitHub Desktop.
Save PCfromDCSnippets/dcfeabb856e1cc576250 to your computer and use it in GitHub Desktop.
Restore SQL Databases
$restoreDir = "J:\Backups"
# Get files in backup directory
$files = get-childitem $restoreDir -recurse
foreach ($file in $files)
{
$query = "RESTORE DATABASE [" + $file.basename + "]
FROM DISK = N'" + $restoreDir + $file + "' WITH FILE = 1,
MOVE N'" + $file.basename + "' TO N'" + $dataLocation + $file.basename + ".mdf',
MOVE N'" + $file.basename + "_log' TO N'" + $logLocation + $file.basename + "_log.LDF',
NOUNLOAD,
STATS = 5"
invoke-sqlcmd -query $query -Database "Master" -ServerInstance $serverInstance -Username "SA" -Password "Password1"
write-host ("$query")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment