title: Renaming a SQL Database using PowerShell does not rename the physical filename
body: <p>I am creating a PowerShell function which can rename a database.</p>
  
  <p>This is working quite well:</p>
  
  <pre><code>    $sqlServer = "MySqlServer"
      $currentDbName = "MyDB"
      $sqlConnectionString = "Server='$sqlserver';Database='$currentDbName';Integrated Security=True"
      $sqlQueries = `
          "ALTER DATABASE [$currentDbName] SET SINGLE_USER WITH ROLLBACK IMMEDIATE",`
          "EXEC sp_renamedb '$currentDbName', '$NewName'",`
          "ALTER DATABASE [$NewName] SET MULTI_USER", `
          "ALTER DATABASE [$NewName] MODIFY FILE (NAME = $currentDbName, NEWNAME = $NewName)", `
          "ALTER DATABASE [$NewName] MODIFY FILE (NAME = $($currentDbName)_Log, NEWNAME = $($NewName)_Log)"
      $conn=new-object System.Data.SqlClient.SQLConnection($sqlConnectionString)
      try {
          $conn.Open()
          $sqlQueries | % {
              $cmd = $conn.CreateCommand()
              $cmd.CommandText = $_
              $cmd.ExecuteScalar()
              Write-Host "    Executed: $_" -ForegroundColor Gray
          }
      }
      catch{
          $actualError = $error[0]
  
          throw $actualError
      }
      finally{
          if($conn) {
              $conn.Close()
              $conn.Dispose()
          }
      }
  </code></pre>
  
  <p>This script rename the database and the datafile (assuming there is only one datafile).</p>
  
  <p>But this script does not rename the physical name of the files and does not supports renaming several data files.</p>
  
  <p>How can also rename the physical files of the database? </p>
  
  <p>Please note that the database schema is not in my control, I can't add stored procedure in the DB.</p>
  
  <p><strong>[Edit]</strong> The script runs remotely, not in the SQL Server box. </p>