title: Run SQL script file from powershell
body: <p>I am trying to run queries stored in a text file from PowerShell. I use following to do that;</p>
  
  <pre><code>Invoke-Expression 'sqlcmd -d TestDB -U $user -P $pw -i "E:\SQLQuery1.sql"'
  </code></pre>
  
  <p>If an error or exception occurs when executing the queries from the <code>.sql</code> file, how can I capture that in my Powershell script? How can I get the script output?  </p>
  
  <p>NOTE: I cannot use <code>invoke-sqlcmd</code></p>