6

Consider "outer.ps1":

"in outer.ps1"
. .\inner.ps1
"in outer.ps1 after sourcing inner.ps1"

that sources "inner.ps1" which just exits:

exit 0

When I run "outer.ps1" the output indicates that the exit only returns from "inner.ps1":

in outer.ps1
in outer.ps1 after sourcing inner.ps1

Is it possible to exit the powershell process from inner.ps1? I don't want to throw an exception, this would be a normal termination.

1 Answer 1

8

You can use this instead:

[Environment]::Exit(0)

But be warned - it will exit the PowerShell console completely. That's a pretty big hammer. :-)

A better way is to use a trap handler in outer.ps1 e.g.:

trap { exit 0 }

And in inner.ps1 do a throw e.g.:

throw "Inner.ps1 done!"
Sign up to request clarification or add additional context in comments.

2 Comments

The hammer is good. I like it, thanks) Just in place to emulate the bash behaviour.
I wish trap printed the throw message but it's ok

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.