Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7956983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:09:36+00:00 2026-06-04T04:09:36+00:00

In PowerShell, what is the difference between $? and $LastExitCode ? I read about

  • 0

In PowerShell, what is the difference between $? and $LastExitCode?

I read about automatic variables, and it said:

$? Contains the execution status of the last operation. It contains TRUE if the last operation succeeded and FALSE if it failed.

$LastExitCode Contains the exit code of the last Windows-based program that was run.

In the definition of $? it doesn’t explain what succeed and fail mean.


I ask because I presumed that $? is True if and only if $LastExitCode is 0, but I found a surprising counter-example: $LastExitCode=0 but $?=False in PowerShell. Redirecting stderr to stdout gives NativeCommandError.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T04:09:38+00:00Added an answer on June 4, 2026 at 4:09 am

    $LastExitCode is the return code of native applications. $? just returns True or False depending on whether the last command (cmdlet or native) exited without error or not.

    For cmdlets failure usually means an exception, for native applications it’s a non-zero exit code:

    PS> cmd /c "exit 5"
    PS> $?
    False
    PS> cmd /c "exit 0"
    PS> $?
    True
    

    Cancelling a cmdlet with Ctrl+C will also count as failure; for native applications it depends on what exit code they set.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The difference between this question and Remove Top Line of Text File with Powershell
In Powershell, is there a difference between & .\monkeys.ps1 and & powershell .\monkeys.ps1 ?
Last time I got confused by the way PowerShell eagerly unrolls collections, Keith summarized
I have a powershell script that gives some status output via write-output . I
I am feeling surprised by the difference between two seemingly identical scripts. first.ps1: A
I'd like to check the difference between two web.config files. How is that possible
PowerShell is a weird mix of .bat and .NET. In .bat, you check errorlevel
Powershell provides the New-WebServiceProxy cmdlet which allows for a web service proxy object to
Do PowerShell scripts run under Mono? I would like to run them on a
In Powershell I am defining a new PSDrive called test . But when I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.