3

Suppose I cd to an empty directory in powershell and run the following command:

get-childitem x

The command will throw an error that it cannot find the path, which is expected.

However, when I check the $LastExitCode it is still zero.

This is confusing to me, since according to the docs $LastExitCode should contain the exit code of the last windows-based program that was run.

Could anyone please explain why the exit code is still zero after I run a command which clearly fails?

2 Answers 2

4

get-childitem doesn't start a new process. If a powershell function or command throws an error it will be stored in the global $Error array. $LASTEXITCODE is created and set when you start a child process, for example a new powershell session with a command:

PS C:\> Get-ChildItem x
Get-ChildItem : Cannot find path 'C:\x' because it does not exist.
At line:1 char:1
+ Get-ChildItem x
+ ~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\x:String) [Get-ChildItem], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

PS C:\> $Error.Count
1
PS C:\> $Error[0]
Get-ChildItem : Cannot find path 'C:\x' because it does not exist.
At line:1 char:1
+ Get-ChildItem x
+ ~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\x:String) [Get-ChildItem], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

PS C:\> $LASTEXITCODE
PS C:\> powershell -Command { get-childitem x }
get-childitem : Cannot find path 'C:\x' because it does not exist.
At line:1 char:2
+  get-childitem x
+  ~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\x:String) [Get-ChildItem], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

PS C:\> $LASTEXITCODE
1
PS C:\> powershell -Command { get-childitem . }


    Directory: C:\


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
.....
.....
.....


PS C:\> $LASTEXITCODE
0
PS C:\>
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for this clarification on the difference between starting a child process and running from the current process.
I would add that there is also $? which could be used if you just want to check if the last command or process was successful (in case of process this means $LASTEXITCODE is not 0). Note that for processes one could not rely on $? until recently (PS 7.1.x or 7.2.x, I'm not sure).
I've checked, $? has been fixed for native processes with PS 7.1 (see breaking changes)
-1

In a powershell script you can do something like this at the bottom:

exit $error.count

Then the exit code will be the number of errors.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.