Tutorial by Examples

Contains process ID of the current hosting process. PS C:\> $pid 26080
$true and $false are two variables that represent logical TRUE and FALSE. Note that you have to specify the dollar sign as the first character (which is different from C#). $boolExpr = "abc".Length -eq 3 # length of "abc" is 3, hence $boolExpr will be True if($boolExpr -eq $tr...
$null is used to represent absent or undefined value. $null can be used as an empty placeholder for empty value in arrays: PS C:\> $array = 1, "string", $null PS C:\> $array.Count 3 When we use the same array as the source for ForEach-Object, it will process all three items (i...
Variable called Output Field Separator contains string value that is used when converting an array to a string. By default $OFS = " " (a space), but it can be changed: PS C:\> $array = 1,2,3 PS C:\> "$array" # default OFS will be used 1 2 3 PS C:\> $OFS = ",.&qu...
Contains the object/item currently being processed by the pipeline. PS C:\> 1..5 | % { Write-Host "The current item is $_" } The current item is 1 The current item is 2 The current item is 3 The current item is 4 The current item is 5 $PSItem and $_ are identical and can be use...

$?

Contains status of the last operation. When there is no error, it is set to True: PS C:\> Write-Host "Hello" Hello PS C:\> $? True If there is some error, it is set to False: PS C:\> wrt-host wrt-host : The term 'wrt-host' is not recognized as the name of a cmdlet, functi...
Array of most recent error objects. The first one in the array is the most recent one: PS C:\> throw "Error" # resulting output will be in red font Error At line:1 char:1 + throw "Error" + ~~~~~~~~~~~~~ + CategoryInfo : OperationStopped: (Error:String) [], R...

Page 1 of 1