Powershell command to check output of last executed command

%3CLINGO-SUB%20id%3D%22lingo-sub-2858037%22%20slang%3D%22en-US%22%3EPowershell%20command%20to%20check%20output%20of%20last%20executed%20command%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2858037%22%20slang%3D%22en-US%22%3E%3CP%3EJust%20like%20in%20Linux%2C%20you%20can%20use%26nbsp%3B%3CSTRONG%3E%24%3F%20%3C%2FSTRONG%3Eto%20find%20the%20output%20of%20last%20command%20executed%2C%20is%20there%20any%20powershell%20command%20that%20I%20can%20use%20on%20the%20command%20line%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20tried%20%3CSTRONG%3E%24%3F%3C%2FSTRONG%3E%20but%20it%20returns%20True%20or%20False.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2858037%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2906426%22%20slang%3D%22en-US%22%3ERe%3A%20Powershell%20command%20to%20check%20output%20of%20last%20executed%20command%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2906426%22%20slang%3D%22en-US%22%3E%3CP%3EHello%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1175600%22%20target%3D%22_blank%22%3E%40Sandeepkv3003%3C%2FA%3E%2C%3C%2FP%3E%3CP%3EAs%20far%20as%20I%20know%2C%20in%20Linux%26nbsp%3B%26nbsp%3B%3CSTRONG%3E%24%3F%3C%2FSTRONG%3E%20returns%26nbsp%3Bexit%20status%20of%20the%20most%20recently%20executed%20foreground%20pipeline.%20%3CA%20href%3D%22https%3A%2F%2Fstackoverflow.com%2Fquestions%2F7248031%2Fmeaning-of-dollar-question-mark-in-shell-scripts%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3E(Reference)%3C%2FA%3E.%3C%2FP%3E%3CP%3E%3CSPAN%3EIn%20PowerShell%20it%20does%20pretty%20much%20the%20same%20returns%20True%20or%20False%20depending%20on%20whether%20the%20last%20command%20(cmdlet%20or%20native)%20exited%20without%20error%20or%20not.%20%3CA%20href%3D%22https%3A%2F%2Fstackoverflow.com%2Fquestions%2F10666035%2Fdifference-between-and-lastexitcode-in-powershell%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3E(Reference)%3C%2FA%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3ENot%20sure%20what%20you%20are%20trying%20to%20accomplish%20and%20what%20your%20use%20case%20is%20but%20I%20suppose%20this%20could%20help%20you%3A%20%3CA%20href%3D%22https%3A%2F%2Fstackoverflow.com%2Fquestions%2F43094708%2Fpowershell-exit-status%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Ehttps%3A%2F%2Fstackoverflow.com%2Fquestions%2F43094708%2Fpowershell-exit-status%3C%2FA%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHope%20that%20helps.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
New Contributor

Just like in Linux, you can use $? to find the output of last command executed, is there any powershell command that I can use on the command line?

 

I tried $? but it returns True or False.

1 Reply

Hello @Sandeepkv3003,

As far as I know, in Linux  $? returns exit status of the most recently executed foreground pipeline. (Reference).

In PowerShell it does pretty much the same returns True or False depending on whether the last command (cmdlet or native) exited without error or not. (Reference)

 

Not sure what you are trying to accomplish and what your use case is but I suppose this could help you: https://stackoverflow.com/questions/43094708/powershell-exit-status

 

Hope that helps.