I can exit a powershell application with exit code by :
exit 59
How can exit with a Custom Exit Code Description?
exit 59, "Somethings Wrong"
You can't. Inside the PowerShell environment, cmdlets can return any .Net object (strings, Exceptions, etc.) to each other, but when you exit
, the PowerShell process exits, and all it can return to Windows / the calling process is an integer.
https://en.wikipedia.org/wiki/Exit_status#Windows
There's nowhere to put a custom description and no way for anything else to get a description. That's years of legacy computing and backwards compatibility constraints back to when processors were small, memory expensive, and return values were (I speculate) one processor register value, or similar.
When you are in a command prompt and run these commands:
c:\> powershell
Windows PowerShell
Copyright (C) 2014 Microsoft Corporation. All rights reserved.
PS C:\> exit 10
c:\> echo %ERRORLEVEL%
10
you see the PowerShell process quit with the return value of 10. But there is no %ERRORDESCRIPTION%
or similar standard feature to put anything else. There's just no lower level machinery for the thing you want to do.
What you can do is write to the standard error output (stderr) using Write-Error and if a program calls your script and captures stderr output, it can read it. Or you can write an error to an event log, or to a logfile.
But any of that writing has to happen while your process is running because, well, you can't do anything after your process has quit.