title: Powershell Try Catch invoke-sqlcmd
body: <p>I am having problems catching an error in PowerShell when a connection fails to a SQL Server using Invoke-Sqlcmd.  This is some generic code to demonstrate the issue:</p>
  
  <pre><code>CLS
  $server = "Localhost\fake"
  try
  {
      Invoke-Sqlcmd -Query "SELECT DB_NAME() as [Database]" -Server $server
  }
  catch
  {
      Write-Host "Error connecting to server " $server
  }
  </code></pre>
  
  <p>I get the following error:</p>
  
  <blockquote>
    <p>Invoke-Sqlcmd : A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name
     is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)</p>
  </blockquote>
  
  <p>I was expecting to get the one line statement: "Error connecting to server Localhost\fake"</p>