title: Can't restore SQL Server database to another server
body: <p>I'm trying to migrate databases from an old, dying server, to the new one.</p>
  
  <p>All over the internet, I found two handy <a href="http://andrewmorgan.ie/2011/12/backup-and-restore-sql-databases-using-powershell/" rel="nofollow noreferrer">scripts</a> by Andrew Morgan, for saving and restoring databases. The backup part works well; I get my <code>db.bak</code> on my local pc by sharing the folder over the network and having the script backup the db to the unc path.</p>
  
  <p>Unfortunately, the restoring part won't work, and I don't know why (and my senior DEV doesn't know either, and he's the one knowing SQL aorund here).</p>
  
  <p>I'm calling it this way:</p>
  
  <pre><code>Restore-SQLdatabase -SQLServer "NEWSERVER\MSSQLSERVER" -SQLDatabase "DBToMigrate" -Path "C:\Backup\DBToMigrate.bak" -SQLusername "sa" -SQLpassword "IMAPLAINTEXTPASSWORD"
  </code></pre>
  
  <p><code>sa</code> has complete power over the database.</p>
  
  <p>It fails with the following message:</p>
  
  <blockquote>
    <p>Attempting to connect to the Specified SQL server: Success<br>
    WARNUNG: An Exception was caught while restoring the database!<br>
    WARNUNG: Ausnahme beim Aufrufen von "ExecuteNonQuery" mit 0 Argument(en):  "Incorrect syntax near '‘'.<br>
    Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon."<br>
    WARNUNG: attempting to recover the database</p>
  </blockquote>
  
  <p>The part in question is:</p>
  
  <pre><code>ALTER DATABASE $SQLDatabase
  SET SINGLE_USER WITH ROLLBACK IMMEDIATE
  
  RESTORE DATABASE $SQLDatabase
  FROM DISK = '$path'
  WITH REPLACE
  </code></pre>
  
  <p>When it gets executed, the scipt gets the exception.</p>
  
  <p>I tried putting a <code>CREATE DATABASE $SQLDatabase</code> in front of the <code>ALTER</code> part, but then it says something along the lines of "that database name already exists", after creating that db.</p>
  
  <p>Also, when omitting the altering part and changing that part to </p>
  
  <pre><code>RESTORE DATABASE $SQLDatabase FROM DISK = '$path' WITH REPLACE
  </code></pre>
  
  <p>It says something about the database files, giving me the path on the old server (which I'd like to be completely irrelevant, but it seems there may be my next problem), cannot be found (of course, since I'm on my local machine now, which doesn't have those).</p>
  
  <p>I'm doing it with a script, because there are around 15 databases we need to migrate, and also because I want to learn how to do this stuff without just using the SSMS wizard.</p>
  
  <p><strong>Update:</strong> I changed the apostrophes to normal ones, that was indeed the mistake. Now I just need to get the file name of the db files so I can <code>MOVE</code> them to the new server.</p>