I'm converting unix scripts into powersshell scripts. while converting i came across the line "exit $rc" in unix script.
I want to know the powershell equivalent for Unix exit command.
Please help me in this regard.
Thanks in advance
There is an exit [<code>]
keyword in PowerShell. If you use exit
in a script, it doesn't matter what scope you are in and even what nesting level, the script will exit. If you execute exit
in the PowerShell console it will exit the PowerShell session. You can use the automatic variable $LastExitCode
to see the value returned by exit.
Don't forget that PowerShell also has exceptions. It might be more appropriate to throw an exception when you encounter an error. That depends on the particulars of the scripts you are porting of course, but it's another tool in the toolbox you can use.