2

I came to know that using '$?' in powershell tells the user whether the previous command was successful in its execution or not by printing 'True' or 'False'.

Request for help/clarification on the following questions please:

First, I want to confirm whether what I stated in the first paragraph is true.

Secondly, if it is possible, a formal documentation on this command would be much welcome.

Thirdly, if what I have stated wrongly on the utility of the '$?' command in powershell, then I would like to know what its actual utility is.

Uma Priyadarsi
  • 85
  • 1
  • 3
  • 11
  • 4
    see [get-help about_automatic_variable](https://technet.microsoft.com/en-us/library/hh847768.aspx?f=255&MSPPError=-2147217396) – Loïc MICHEL Aug 24 '15 at 10:31

1 Answers1

2
  1. Yes, it's true.

  2. @Kayasax has provided you an official doc from technet.microsoft.com

Try it on your own: enter image description here

Example (credits to yonglianglee):

? (dollar sign + question mark) Returns True or False value indicating whether previous command ended with an error. For some reason it does not catch all errors, but most of the time it works.

Task 1: See if a powershell cmdlet exists in the system. Code.

SomeCmdLet #does not exists
$?
$?

Output

The term 'SomeCmdLet' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:15
+ SomeCmdLet <<<<  #does not exists
+ CategoryInfo          : ObjectNotFound: (SomeCmdLet:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
False    #error occured - previous cmdlet (SomeCmdLet) was not found
True     #no errors returned by the previous command ($?)

Task 2: See if a WMI class exists in the system

gwmi win32_processo -ErrorAction SilentlyContinue   #intentional error, win32_processor is the right one
$?
$?

Output:

False
True
Pawel Gumiela
  • 1,992
  • 2
  • 20
  • 36