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.
The Splatting Operator To create an array, we create a variable and assign the array. Arrays are noted by the "@" symbol.
$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.
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\ .
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With