Last active
December 22, 2015 16:59
-
-
Save k4gdw/6502922 to your computer and use it in GitHub Desktop.
Powershell 2 script to restore a bunch of Sql Server databases.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# 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