title: Returning an exit code from a PowerShell script
body: <p>I have questions about returning an exit code value from PowerShell when run from a cmd.exe invocation. I found <a href="https://weblogs.asp.net/soever/returning-an-exit-code-from-a-powershell-script" rel="noreferrer">https://weblogs.asp.net/soever/returning-an-exit-code-from-a-powershell-script</a> which has been helpful. But, the solution for PowerShell code is to add a function.</p>
  
  <pre><code>function ExitWithCode { param($exitcode) $host.SetShouldExit($exitcode) exit }
  </code></pre>
  
  <p>Generates:</p>
  
  <pre><code>ExitWithCode : The term 'ExitWithCode' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path
  was included, verify that the path is correct and try again.
  At C:\src\t\e\exit5.ps1:6 char:1
  + ExitWithCode -exitcode 12345
  + ~~~~~~~~~~~~
      + CategoryInfo          : ObjectNotFound: (ExitWithCode:String) [], CommandNotFoundException
      + FullyQualifiedErrorId : CommandNotFoundException
  </code></pre>
  
  <p>But, placing the "exit" on a new line works. Is this just a language anomaly?</p>
  
  <pre><code>function ExitWithCode { param($exitcode) $host.SetShouldExit($exitcode)
  exit }
  </code></pre>
  
  <p>Also, this page is from 2010. Is this still the current state? Is there a better/easier way now?</p>