Powershell restore SQL Server database to new database

Question:

I have a database $CurrentDB and I want to restore a backup of $CurrentDB to $NewDB. The T-SQL command looks like this:

I am attempting to user Restore-SqlDatabase but I don’t know how to properly -RelocateFile

I can’t seem to locate an example of what I am attempting to do. I have seen plenty of examples of restoring databases with the same name but different files. I want a different name and different file names. I am open to suggestions.

Answer:

You don’t have to use SMO just because your’re in PowerShell.

And if you don’t have sqlps installed, you can use System.Data.SqlClient from Powershell to run TSQL.

Source:

Powershell restore SQL Server database to new database by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply