Sql-server – the best powershell script to restore an SQL Database

powershellsql server

To restore an SQL Server 2008 database, I would lile to be able to just do something like this in powershell:

./restore.ps1 DatabaseName.bak

Then the powershell script would by convention restore it to a database with name "DatabaseName". It would disconnect any user connected to this database so that it can restore the DB. It would store the mdf and ldf in the default location.

This would mainly be while developing on my personal machine. Just a quick way to restore a DB.

Anyone has such a script?

Thanks

Best Answer

I created a script and posted it on PoshCode which should meet your requirements. The script uses the SQLPSX sqlserver module. Note I had to add a restore file list only option to the invoke-sqlrestore function, so you'll need to grab the module from source code section rather than download section. This will be added to a future release of SQLPSX:

Module: http://sqlpsx.codeplex.com/SourceControl/changeset/view/58378#564810

Script: http://poshcode.org/2530