title: How to return an exit code from a Powershell script only when run non-interactively
body: <p>I have a lot of scripts that are running as scheduled tasks. So they do a <code>$host.setshouldexit(1)</code> on any failure, which shows up in the task scheduler as the return code.</p>
  
  <p>I also want to be able to run these scripts interactively while debugging and testing. So the <code>$host.setshouldexit()</code> kills my powershell or ISE session.</p>
  
  <p>My question is: how can I detect if a script is running non-interactively? If it is, then I'll use <code>setshouldexit</code>, otherwise it will print the error code or something nondestructive. (Note that I don't want to use <code>[environment]::userinteractive</code> because these scripts are not always running in what the OS thinks is a non-interactive session.)</p>
  
  <p>There is a -noninteractive switch that I'm using for the scheduled tasks. Is there some way I can query that from powershell?</p>