title: How to suppress output and check whether or not a command is successful?
body: <p>I am trying to write a powershell script that tests if a MySQL login is successful by using <code>$?</code> to check if an error occurs. </p>
  
  <p>I also want to suppress all output - successful or not successful - from the command.</p>
  
  <p>These are the things I've tried:</p>
  
  <pre><code>mysql -u root --password=mypass -e "show databases"
  If ( $? ) {
    echo "Hooray!"
  } Else {
    echo "Boo!"
  }
  </code></pre>
  
  <p>This works correctly but doesn't suppress any output. </p>
  
  <pre><code>mysql -u root --password=mypass -e "show databases" &gt; $null
  </code></pre>
  
  <p>Works correctly still but does not suppress the errors if the password is wrong.</p>
  
  <pre><code>mysql -u root --password=mypass -e "show databases" 2&gt; $null
  </code></pre>
  
  <p>This does not work correctly. In this example, it always prints "Boo!"</p>
  
  <pre><code>mysql -u root --password=mypass -e "show databases" &gt; $null 2&gt;&amp;1
  </code></pre>
  
  <p>This suppresses all output correctly but only ever prints "Boo!" like before.</p>