3

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

2 Answers 2

5

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.

2
  • In unix code a=10, b=20, [$a -eq $b], rc=$?, exit $rc; What is the equivalent code in powershell for above code?
    – Raj
    Nov 27, 2013 at 10:54
  • 1
    @user2990846 Please do not move the target. If you have an additional question: post an additional question. However, SO is not a free translation service, so you'll eventually need to read a tutorial to get accustomed to the language, which is quite similar to bash in some ways, but fundamentally different in others. Nov 27, 2013 at 14:03
3

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.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.