# SQL - Powershell Restore of a database

**URL:** <https://forums.powershell.org/t/sql-powershell-restore-of-a-database/9054>\
**Category:** PowerShell Help\
**Created:** [July 24, 2017, 6:50am UTC](https://forums.powershell.org/t/sql-powershell-restore-of-a-database/9054 "2017-07-24T06:50:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![imprezaclaire](https://avatars.discourse-cdn.com/v4/letter/i/90ced4/32.png) [@imprezaclaire](https://forums.powershell.org/u/imprezaclaire)\
**Post date:** [July 24, 2017, 6:50am UTC](https://forums.powershell.org/t/sql-powershell-restore-of-a-database/9054/1 "2017-07-24T06:50:16Z")

</div>

This is my script. I am trying to restore a sql backup over to another database. When i run this. This tried to override the original database even though i am specifying a new database. See my script below:

[System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SqlServer.SMO”) | Out-Null  
[System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SqlServer.SmoExtended”) | Out-Null  
[System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SqlServer.ConnectionInfo”) | Out-Null  
[System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SqlServer.SmoEnum”) | Out-Null

$File =gci c:\sqlbackups -include cstest.bak -Recurse  
$CrockfordFile = gci c:\sqlbackups -include cstest.bak -Recurse

copy-Item $file -Destination C:\Webpub\Crockford

Import-Module SQLPS -DisableNameChecking  
$svr = New-Object (“Microsoft.SqlServer.Management.Smo.Server”) “SUPPORTPC14\SQLEXPRESS”;  
{  
$svr.KillAllProcesses(“cstest”)  
$svr.KillDatabase(“cstest”)  
}

Restore-SqlDatabase -Serverinstance “SUPPORTPC14\SQLEXPRESS” -database “CSTest” -BackupFile $CrockfordFile -ReplaceDatabase

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [July 24, 2017, 5:38pm UTC](https://forums.powershell.org/t/sql-powershell-restore-of-a-database/9054/2 "2017-07-24T17:38:16Z")

</div>

Yeah, SMO can be a bit tricky. I’ll tell you - you may get a better answer someplace like StackOverflow, because you’re really using SMO, which is part of .NET, and not really using “native” PowerShell stuff, which is what we specialize in here.

I do know that there’s a complex override needed in order to keep a backup from going back to its original location, but I’ve never needed to code for that myself.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:37pm UTC](https://forums.powershell.org/t/sql-powershell-restore-of-a-database/9054/3 "2024-05-16T20:37:52Z")

</div>

Closed on May 16, 2024, 8:37pm UTC


