What is $? alias for Powershell?

Inside the script, it seemed to me that there was a line today:

If ($?) { #do some stuff } 

I've never seen a $ dollar sign question mark sign? before and I can’t install through Google what it is intended for.

When I run it in a powershell window, it usually returns True, but sometimes returns False. My testing showed that it returns False when the code preceding it executes in an error (and in the context of the script, I saw that this might make sense), so this is probably an alternative way to handle TRY. CATCH script.

Example:

 PS C:\Users\me> $? True PS C:\Users\me> $? True PS C:\Users\me> blah blah : The term 'blah' 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:1 + blah + ~~~~ + CategoryInfo : ObjectNotFound: (blah:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException PS C:\Users\me> $? False PS C:\Users\me> $? True 

Can someone check me if this is the case, or if it serves some other purpose?

+7
variables alias powershell
source share
1 answer

From about_automatic_variables :

$?
Contains the status of the last operation. This contains TRUE if the last operation was successful, and FALSE if it failed.

 Get-Help about_automatic_variables 
+11
source share

All Articles