Actually you can set the code also with throw, just put it before throw.
Contents of the script: Throw.ps1
exit 222
throw "test"
Output:
PS C:\> .\Throw.ps1
PS C:\> $LASTEXITCODE
222
If you run it like this:
powershell .\Throw.ps1
The output will be as follows:
PS C:\> powershell .\Throw.ps1
PS C:\> $LASTEXITCODE
1
But here is the thing, powershell exit codes should be either 0 or 1,
anything else, finally will give you result of 1.
Another interesting thing to mention, if to try $? after you run the script,
if true or false, the result depends of what you want to put in there.
exit 0 --> true
, exit 1 --> false
Here it is an example:
Content of the script: Throw_1.ps1
exit 1
throw "test"
Output:
PS C:\> .\Throw_1.ps1
PS C:\> $?
False
Content of the script: Throw_0.ps1
exit 0
throw "test"
Output:
PS C:\> .\Throw_0.ps1
PS C:\> $?
True
As you can see it is just what you need or want to achieve.