Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "$?" mean in PowerShell

I'm very new to PowerShell. While looking up information about error handling I've found a lot of references to using "$?"

I know that it has something to do with errors but what exactly is it? And where can I read more about it?

All of my Google searches have found nothing.

like image 874
Schuyler Avatar asked Jun 21 '13 14:06

Schuyler


People also ask

What does @{} mean in PowerShell?

The Splatting Operator To create an array, we create a variable and assign the array. Arrays are noted by the "@" symbol.

What does $true mean in PowerShell?

$true is effectively the PowerShell "literal" (technically a constant variable) for a boolean. Coercion is common for dynamic languages that allow a range of conditions to be simplified; look at JavaScript and others. It's simply a direction of language design, not merely a mistake.

What does recurse mean in PowerShell?

The Recurse parameter gets items from the Path directory and its subdirectories. For example, -Path C:\Test\ -Recurse -Include *.txt. If a trailing asterisk ( * ) isn't included in the Path parameter, the command doesn't return any output and returns to the PowerShell prompt. For example, -Path C:\Test\ .


1 Answers

From the The Essential Windows PowerShell Cheat Sheet:

Errors and Debugging: The success or failure status of the last command can be determined by checking $?

Example:

> Get-Content file-that-exists.txt
Hello world
> Write-Host $?
True
> Get-Content file-that-does-not-exist.txt
Get-Content : Cannot find path 'C:\file-that-does-not-exist.txt' because it does not exist.
At line:1 char:1
+ Get-Content file-that-does-not-exist.txt
+ ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\file-that-does-not-exist.txt:String) [Get-Content], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
> Write-Host $?
False
like image 71
Klas Mellbourn Avatar answered Oct 03 '22 01:10

Klas Mellbourn