Tutorial by Examples

Get-ChildItem -Path $PSScriptRoot This example retrieves the list of child items (directories and files) from the folder where the script file resides. The $PSScriptRoot automatic variable is $null if used from outside a PowerShell code file. If used inside a PowerShell script, it automatically ...
$Args Contains an array of the undeclared parameters and/or parameter values that are passed to a function, script, or script block. When you create a function, you can declare the parameters by using the param keyword or by adding a comma-separated list of parameters in parentheses after the...
Get-Process | ForEach-Object -Process { $PSItem.Name } Same as $_. Contains the current object in the pipeline object. You can use this variable in commands that perform an action on every object or on selected objects in a pipeline.

$?

Get-Process -Name doesnotexist Write-Host -Object "Was the last operation successful? $?" Contains the execution status of the last operation. It contains TRUE if the last operation succeeded and FALSE if it failed.
Get-Process -Name doesnotexist Write-Host -Object ('The last error that occurred was: {0}' -f $error[0].Exception.Message) Contains an array of error objects that represent the most recent errors. The most recent error is the first error object in the array ($Error[0]). To prevent an error fr...

Page 1 of 1