-8

What is the meaning of this line in PowerShell?

I have tried searching on google but I do not see any specific explanations.

Duy Gonzales
  • 43
  • 2
  • 12
  • 4
    Documentation: https://learn.microsoft.com/en-us/dotnet/api/system.environment.exit – Bill_Stewart Aug 05 '19 at 17:12
  • Generally, **do _not_ use `[Environment]::Exit()` in your PowerShell code**: it not only sets the desired exit code, but _instantly terminates_ the _entire process_, and can even bypass cleanup code, as [stated in the documentation](https://learn.microsoft.com/en-us/dotnet/api/system.environment.exit). Therefore, if, from within a PowerShell session, you call a script that calls `[Environment]::Exit()`, the *entire session* exits. **Instead, use `exit `**, where `` is the desired exit code - see [this answer](https://stackoverflow.com/a/57468523/45375). – mklement0 Aug 12 '19 at 21:36
  • As for how you could have discovered the API documentation yourself: `[Environment]::Exit(0)` implies the use of .NET type (`[...]`) `System.Environment` (PowerShell allows you to omit the `System.` part) and its static (`::`) `.Exit()` method, so you could have googled `System.Environment.Exit` to find the API documentation. – mklement0 Aug 12 '19 at 21:39

1 Answers1

1

Classically, programs in MS-DOS and under the Windows Command Line (CMD.EXE) signalled errors by setting the system environment variable ERRORLEVEL to a non-zero value. PowerShell does not, by default, do this. If one wishes to invoke a PowerShell script, and have it behave like other programs (and batch files) when called from a batch file, the call to [Environment]::Exit() allows you to set ERRORLEVEL in a way that is compatible with CMD.EXE's expectations. You can see more about [Environment]::Exit() at Microsoft Docs.

Jeff Zeitlin
  • 9,773
  • 2
  • 21
  • 33
  • PowerShell, by default, does do this. If your program ends, it returns zero. If an unhandled exception is thrown, you get a return one. When you use the `exit` keyword, you're specifying that exit code. – Maximilian Burszley Aug 05 '19 at 19:25
  • @TheIncorrigible1 - That's not entirely consistent with my experience; _some_ unhandled exceptions do this, but if it's an error that can be handled (e.g., with try/catch/finally), even if it's unhandled, I can't say I've consistently gotten a non-zero ERRORLEVEL. – Jeff Zeitlin Aug 05 '19 at 20:07
  • More generally, exit codes are how _processes_ signal success vs. failure to one another. Calling from `cmd.exe` is probably becoming less and less important, but calls to the PowerShell CLI from build/automation/CI servers are more important than ever. Note that `ERRORLEVEL` is _not_ a true environment variable - it's just how `cmd.exe` surfaces a child process' exit code. Use of `[Environment]::Exit()` is problematic, because it unconditionally exits the _entire process_, so it shouldn't be used for code that may _also_ be called from a PowerShell session. – mklement0 Aug 12 '19 at 21:48
  • 1
    As for how PowerShell sets its own exit code: the rules are somewhat complex, but I've tried to summarize them in the bottom section of [this answer](https://stackoverflow.com/a/57468523/45375). /cc @TheIncorrigible1 – mklement0 Aug 12 '19 at 21:50