title: Powershell - How to call cmdlet in non-interactive way in Powershell script?
body: <p>In my powershell script, I am calling a cmdlet (Say "Connect-Database"). If the cmdlet needs additional parameter, it prompts the user to supply those values ( say, "Connect-Database" can not get credentials from registry , so it prompts for "Username" "password" on Powershell console and expects the user to give that value ).</p>
  
  <p>I looked into the code of Powershell cmdlet. I found that it is using "CommandInvocationIntrinsics" to prompt the user (using "NewScriptBlock" and then "Invoke" method of "CommandInvocationIntrinsics" ).</p>
  
  <p>Since I am calling this cmdlets from the Powershell script, I want that whenever such a prompting happened, it will be suppressed and an exception is thrown.</p>
  
  <p>The code is something like this -</p>
  
  <pre><code>try
  {
      $db = Connect-Database &lt;databasename&gt;
      #if username / password is prompted, it should be converted into error. But it should not be shown in powershell console.
  
      if($Error.Count &gt; 0)
      {
          throw $Error[0]
      }
  }
  catch
  {
      #do something
  }
  </code></pre>