What is `$?` in Powershell?

powershell examples
in powershell script
in powershell
powershell tutorial
powershell array
powershell syntax
powershell punctuation
using in powershell

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.

It returns true if the last command was successful, else false.

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.

PS> Get-ChildItem 

PS> $? 
    True

$? 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.

PS> Get-ChildItem \Some\Directory\Which\Does\Not\Exist
    Get-ChildItem : Cannot find path 'C:\Some\Directory\Which\Does\Not\Exist' because it does not exist.

PS> $?
    False

$? will return False here, as the previous command was not successful.

What is PowerShell?, An operator is a language element that you can use in a command or expression. PowerShell supports several types of operators to help you  The point is that the dollar sign in $_. indicates that $_. is a variable. However, this is no ordinary variable. Instead, it is often referred to as a variable in the pipeline. In PowerShell, the word pipeline generally refers to a series of commands that have been joined together.

$? will contain $false if the last command resulted in an error. It will contain $true if it did not. In the PowerShell v1 days, this was a common way to do error handling. For example, in a script, if you wanted to check for the existence of a file and then print a custom message if it did not, you could do:

Get-Item -Path john -ErrorAction silentlycontinue;
if( -not $?)
{
    'could not find file.';
     exit
 }`

about_Operators, I made myself this PowerShell Punctuation Cheat Sheet. Hopefully it helps you out too! Symbol, Name, Function, Example. #, Pound or Hash  Examples of the PowerShell $_ Variable. PowerShell’s $_ Variable; The Significance of the Dot in PowerShell’s $_. $_ Example to Filter WmiObjects; PowerShell $_ with ForEach $_. More PowerShell Variables; PowerShell’s $_ Variable. The first point to remember is that $_ is a variable or placeholder.

You can also access last commands exit code using $LastExitCode parameter.

# run some command
# ...
if ((! $?) -and $ErrorAction -eq "Stop") { exit $LastExitCode }

A Cheat Sheet for All the *{_(%#$] PowerShell , PowerShell is the shell framework developed by Microsoft for administration tasks such as configuration management and automation of  PowerShell provides special variables, such as $args, which is an array of all the command line arguments passed to a function from the command line, and $_, which refers to the current object in the pipeline. PowerShell also provides arrays and associative arrays.

I have encountered in Windows Server 2019, $? can be set false when Standard Error has been generated. In my example docker-compose logs warnings via Standard Error, so although exiting with 0, $? indicates failure.

What are PowerShell Commands? Code Examples, Tutorials & More, results of the script, but when he looked at the PS1 file (PS1 is the file extension that most PowerShell scripts use), it may as well have been written in Greek. If you break down powershell to basics then everything is a script block including a script file a, functions and cmdlets. You can define your own parameters but in some occasions one is created by the system for you that represents the input item to process/evaluate .

[PDF] Five Simple Symbols You Should Know to Unlock Your PowerShell , Powershell - Scripting - Windows PowerShell is a command-line shell and scripting language designed especially for system administration. Its analogue in​  Describes the operators that are supported by PowerShell. Long description. An operator is a language element that you can use in a command or expression. PowerShell supports several types of operators to help you manipulate values. Arithmetic Operators. Use arithmetic operators (+, -, *, /, %) to calculate values in a command or expression

Powershell - Scripting, One of the expressions that I tend to use in my PowerShell scripts on a regular basis is dollar sign underscore dot, (or $_.). Recently, I had  Windows PowerShell is a command-line shell and scripting language designed especially for system administration. Its analogue in Linux is called as Bash Scripting. Built on the .NET Framework, Windows PowerShell helps IT professionals to control and automate the administration of the Windows operating system and applications that run on Windows Server environment.

What does $_. mean in PowerShell?, Task 1: See if a powershell cmdlet exists in the system. Code. SomeCmdLet #​does not exists $? $? Output. The term 'SomeCmdLet  Windows PowerShell. The first version of PowerShell was released in November 2006 for Windows XP, Windows Server 2003 and Windows Vista. The latest version of PowerShell is Windows PowerShell 5.1, and it is delivered 2016 as part of Windows 10 Anniversary Update and Windows Server 2016.

Comments
  • In Powershell, $? is an example of an "automatic variable." It helped me to know the nomenclature.
  • @MattHickford A command is something you execute using Powershell. It can be a cmdlet or an executable for example. See the example I've added.
  • What does 'successful' mean? Do you think it means 'if and only if $LastExitCode is 0'?
  • I've found a counter-example where $? is False but $LastExitCode is 0. See stackoverflow.com/questions/10666101/…
  • The article link in the answer is dead.
  • @RobbVandaveer Thanks - I've linked to archive.org now.
  • What do people do now instead?
  • @mikemaccana, in the same boat here.... I would think try/catch statements.
  • try/catch is available in PS, but exceptions are only 'caught' when there is a terminating error. Often, a cmdlet won't terminate the execution but will continue on. In these cases, $? is you're alternative.
  • I'm pretty sure you just saved me from an exploding artery in my head. I'm running a powershell script in a Jenkins build that executes Selenium tests. A Chrome browser and web driver upgrade just started logging an error, but the test passed. The $? variable was $False but $LastExitCode was zero. This build has been failing for a while now, and this answer finally pinpointed the failure. Basically don't trust $?. Use $LastExitCode instead.