title: Restore SQL database - Incorrect syntax near '-'
body: <p>I'm trying to restore a database with a <code>-</code> in the name. If I have no <code>-</code> in the name, the database is correctly restored. </p>
  
  <p>If I put <code>''</code> around the database it still doesn't work. I'm still searching what to do but I can't find it. </p>
  
  <pre><code>$sqlRestoreCommand.CommandText = "RESTORE DATABASE '$databaseName' FROM DISK = '$databaseBackup' " ;
  </code></pre>
  
  <p>The following exception occurs and the code is below:</p>
  
  <blockquote>
    <p>Exception: Exception calling "ExecuteNonQuery" with "0" argument(s): "Incorrect syntax near '-'.
    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.".Exception.Message</p>
  </blockquote>
  
  <pre><code>$databaseName = "TempRpc-RC";
  
  # Write the state for debugging purposes.
  "SQL State" &gt; sqlstatelog.txt;
  $connection.State &gt;&gt; sqlstatelog.txt;
  
  # Create the SQL restore command.
  $sqlRestoreCommand = $connection.CreateCommand();
  
  # Set the command text to a SQL restore command and fill in the parameters.
  # With move option is needed!
  $sqlRestoreCommand.CommandText = "RESTORE DATABASE $databaseName FROM DISK = '$databaseBackup' " ;
  $sqlRestoreCommand.CommandText += "WITH FILE = 1, ";
  $sqlRestoreCommand.CommandText += "MOVE N'$databaseName" + "'" + " TO N'C:\Program Files\Microsoft SQL Server\MSSQL14.SQLEXPRESS\MSSQL\Data\$databaseName" + "_Data.mdf', ";
  $sqlRestoreCommand.CommandText += "MOVE N'$databaseName" + "_log'" +" TO N'C:\Program Files\Microsoft SQL Server\MSSQL14.SQLEXPRESS\MSSQL\Data\$databaseName" +"_Log.ldf'";
  $sqlRestoreCommand.Connection = $connection;
  
  # Execute the commands.
  $sqlRestoreResult = $sqlRestoreCommand.ExecuteNonQuery();
  
  # Write the query
  Write-Verbose $sqlRestoreCommand.CommandText;
  </code></pre>