Skip to content

Instantly share code, notes, and snippets.

@k4gdw
Last active December 22, 2015 16:59
Show Gist options
  • Save k4gdw/6502922 to your computer and use it in GitHub Desktop.
Save k4gdw/6502922 to your computer and use it in GitHub Desktop.
Powershell 2 script to restore a bunch of Sql Server databases.
<# restoreDBs.ps1
.SYNOPSIS
A script to restore multiple databases on SqlServer from this
Winsmarts.com afticle: http://bit.ly/17lTl6t
.DESCRIPTION
The script enumerates the .bak files in the current directory
and restores each file to a separate database named the same
as the file, minus the ".bak" extension. For example, this
file, "AuthDB.bak" will yield a database named "AuthDB"
.NOTES
File Name : restoreDBs.ps1
Author : Sahil Malik - http://bit.ly/17lTXJs
: With minor change to use [IO.Path]::Combine
: to assemble the $dbPath variable as suggested
: by a commenter.
Prerequisite : PowerShell V2 over Vista and upper.
Copyright 2013 - Sahil Malik
#>
$path = (get-location).path
$files = get-childitem | where {$_.extension -eq ".bak"}
foreach($file in $files)
{
$dbPath = [IO.Path]::Combine($path,$file)
$dbName = $file.ToString().Replace(".bak", "")
write-host "Adding" $dbPath "to db " $dbName
Restore-SqlDatabase -ServerInstance "writeyourserverinstancehere" -Database $dbName -BackupFile $dbPath
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment