PowerShell supports the "EXIT" keyword, which is scoped and may be different from the way you think it works.
Let's test this function:
Copy Code code as follows:
function test
{
A
Exit
B
}
When you save and invoke the script, you will get:
Copy Code code as follows:
Ps> C:\USERS\TOBIAS\DOCUMENTS\POWERSHELL\TEST12343.PS1
A
At this point "exit" prematurely ended this function. But when you don't save the current script or when you call an interaction function, your entire PowerShell host will shut down.
"Exit" ends the currently invoked script, not just a function. If you save it to a script like the following, you might find something unexpected:
Copy Code code as follows:
function test
{
A
Exit
B
}
' Start '
Test
' Stop '
Now it turns out like this:
Copy Code code as follows:
Ps<> C:\USERS\TOBIAS\DOCUMENTS\POWERSHELL\TEST12343.PS1
Start
A
Note that the code "Stop" is not executing. "Exit" ends its own function and stops subsequent execution of the scope. If calling an interaction function why not close PowerShell? (This is because, you invoke the scope host is itself).
So how do you use "exit"? You can set a number of error levels while the script is working. This rank number can be obtained from the call. So if you create a PowerShell scheduled task, or execute a batch file by Powershell.exe, then the different numbers you specify after "Exit" will become the exit code outside your script as you would in the batch of%errorlevel%.