What is the meaning of $?
in Powershell?
Edit: TechNet answers in tautology, without explaining what 'succeed' or 'fail' mean.
$?
Contains the execution status of the last operation. It contains TRUE if the last operation succeeded and FALSE if it failed.
I presumed $? would simply test whether $LastExitCode is 0, but I found a counter example where $? is False but $LastExitCode is True.
Best Answer
It returns
true
if the last command was successful, elsefalse
.However, there are a number of caveats and non-obvious behaviour (e.g. what exactly is meant by "success"). I strongly recommend reading this article for a fuller treatment.
For example, consider calling Get-ChildItem.
$? will return
True
as the call to Get-ChildItem succeeded.However, if you call Get-ChildItem on a directory which does not exist it will return an error.
$? will return
False
here, as the previous command was not successful.