none
Launch exe from Powershell and get the return code

    Question

  • If I run an exe from powershell. how do I get the return code of whether the process executed successfully or with error?

    Can you give me a sample of this?


    MSDNStudent Knows not much!
    Monday, November 14, 2011 8:15 AM

Answers

  • $LastExitCode or $?
    • Marked as answer by MSDN Student Monday, November 14, 2011 1:42 PM
    Monday, November 14, 2011 8:22 AM
  • If I run an exe from powershell. how do I get the return code of whether the process executed successfully or with error?

    Can you give me a sample of this?


    MSDNStudent Knows not much!

    Hi,

     

    PowerShell can check the status of the variable $? Or $lastexitcode, the $? variable will be true if the entire operation succeeded, and false otherwise. If any operations generated an error object, then $? Will be set to false.

     

    Here’s an example:

    Copy.exe is a native command in windows system, so we can use it to do this test.

     

    PS> copy abc  def

     

    Copy-Item : Cannot find path 'C:\Users\abc' because it does not exist.

    At line:1 char:5

    + copy <<<<  abc def

        + CategoryInfo          : ObjectNotFound: (C:\Users\abc:String) [Copy-Item], ItemNotFoundException

        + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyItemCommand

     

    PS> $?

    False

     

    In this example, We got the expected error and $? is “False”.

     

    PS> copy E:\54.jpg E:\test

    PS> $?

    True

     

    Since, no error occurs and $? is “True”.

     

    Please feel free to ask me if you have any questions.   

     

    Best Regards,

    Anders Wang


    • Proposed as answer by Bigteddy Monday, November 14, 2011 10:24 AM
    • Edited by Anders_WangModerator Monday, November 14, 2011 12:51 PM
    • Marked as answer by MSDN Student Monday, November 14, 2011 1:45 PM
    Monday, November 14, 2011 10:08 AM
    Moderator
  • To add to this, while $? will return a boolean (true or false), $lastexitcode will return the actual result code sent from the called application.

    Some applications, such as 7zip, will return a variety of $lastexitcode(s), depending on the result of the operation.  You can then return a message to the user depending on the code.

     


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    • Marked as answer by MSDN Student Monday, November 14, 2011 1:45 PM
    Monday, November 14, 2011 10:30 AM

All replies

  • $LastExitCode or $?
    • Marked as answer by MSDN Student Monday, November 14, 2011 1:42 PM
    Monday, November 14, 2011 8:22 AM
  • If I run an exe from powershell. how do I get the return code of whether the process executed successfully or with error?

    Can you give me a sample of this?


    MSDNStudent Knows not much!

    Hi,

     

    PowerShell can check the status of the variable $? Or $lastexitcode, the $? variable will be true if the entire operation succeeded, and false otherwise. If any operations generated an error object, then $? Will be set to false.

     

    Here’s an example:

    Copy.exe is a native command in windows system, so we can use it to do this test.

     

    PS> copy abc  def

     

    Copy-Item : Cannot find path 'C:\Users\abc' because it does not exist.

    At line:1 char:5

    + copy <<<<  abc def

        + CategoryInfo          : ObjectNotFound: (C:\Users\abc:String) [Copy-Item], ItemNotFoundException

        + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyItemCommand

     

    PS> $?

    False

     

    In this example, We got the expected error and $? is “False”.

     

    PS> copy E:\54.jpg E:\test

    PS> $?

    True

     

    Since, no error occurs and $? is “True”.

     

    Please feel free to ask me if you have any questions.   

     

    Best Regards,

    Anders Wang


    • Proposed as answer by Bigteddy Monday, November 14, 2011 10:24 AM
    • Edited by Anders_WangModerator Monday, November 14, 2011 12:51 PM
    • Marked as answer by MSDN Student Monday, November 14, 2011 1:45 PM
    Monday, November 14, 2011 10:08 AM
    Moderator
  • To add to this, while $? will return a boolean (true or false), $lastexitcode will return the actual result code sent from the called application.

    Some applications, such as 7zip, will return a variety of $lastexitcode(s), depending on the result of the operation.  You can then return a message to the user depending on the code.

     


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    • Marked as answer by MSDN Student Monday, November 14, 2011 1:45 PM
    Monday, November 14, 2011 10:30 AM
  • "Copy.exe is a native command in windows system, so we can use it to do this test."

    But Copy is also an alias for the Powershell cmdlet Copy-Item as the rest of your post shows....

    In fact, there is no copy.exe at a vanilla Windows 7 system; it is a builtin command in cmd.exe....

    I can't believe somebody at MS, also a moderator gives answers like this and that it gets voted as answer too...

    Thursday, July 13, 2017 4:31 PM
  • "Copy.exe is a native command in windows system, so we can use it to do this test."

    But Copy is also an alias for the Powershell cmdlet Copy-Item as the rest of your post shows....

    In fact, there is no copy.exe at a vanilla Windows 7 system; it is a builtin command in cmd.exe....

    I can't believe somebody at MS, also a moderator gives answers like this and that it gets voted as answer too...

    Because your post has nothing to do with the question that was asked. The question is about detecting errors. It was about detecting erros with an external command.

    $LastExitCode and $? are both in play here.

    The one issue is that the example is missing the CMD/C decoration.

    CMD/C copy abc xyz
    $LASTEXITCODE

    In 2011 many help desk people at MS had not been trained in PowerShell.  The forums seem to be a place for MS to dump newbies.  Many are also sales support and not techs but are sent here as moderators to learn how to work with technical customers.

    Be nice to the MS newbies.  They may be annoying but they will learn ... I think.

    And stop responding to decade old threads.  You may be accused necrophilia.


    \_(ツ)_/

    Thursday, July 13, 2017 5:31 PM
    Moderator