Automatic Variables

Defined and set automatically by PowerShell:

  $_  - The current pipeline object.
  $?  - True if last operation succeeded and False otherwise.
  $$  - The last token of the last line received by the shell.
  $^  - The first token of the last line received by the shell.
  $Args  - An array of the parameters passed to a function.
  $DebugPreference  - The action to take when data is written using Write-Debug/WriteDebug.
  $Error            - Objects for which an error occurred while being processed in a cmdlet.
  $ErrorActionPreference  - The action to take when data is written using Write-Error/WriteError.
  $foreach  - Refers to the enumerator in a foreach loop.
  $Home     - The user's home directory. Equivalent of  %homedrive%%homepath%.
  $Input    - Use in script blocks that are in the middle of a pipeline.
  $LASTEXITCODE  - The exit code of the last Win32 executable execution.
  $MaximumAliasCount  - Maximum no. of aliases available to the session.
  $MaximumDriveCount  - Maximum no. of drives available, excluding those provided by the OS.
  $MaximumFunctionCount  - Maximum no. of functions available to the session.
  $MaximumHistoryCount   - Maximum no. of entries saved in the command history.
  $MaximumVariableCount  - Maximum no. of variables available to the session.
  $null   - Null a scalar value that contains nothing.
  $PsHome - The directory where PowerShell is installed.
  $Host   - Information about the current host.
  $OFS    - Output Field Separator, used when converting an array to a string. default= space character. 
  $ReportErrorShowExceptionClass  - TRUE = show the class name of displayed exceptions.
  $ReportErrorShowInnerException  - TRUE = show the chain of inner exceptions.
  $ReportErrorShowSource      - TRUE = show the assembly name of displayed exceptions.
  $ReportErrorShowStackTrace  - TRUE = emit the stack trace of exceptions.
  $ShouldProcessPreference    - Action to take when ShouldProcess is used in a cmdlet.
  $ShouldProcessReturnPreference  - Value returned by ShouldPolicy
  $StackTrace - Detailed stack trace information about the last error.
  $VerbosePreference  - The action to take when data is written using Write-Verbose/WriteVerbose.
  $WarningPreference  - The action to take when data is written using Write-Warning/WriteWarning.

Example

Setting $OFS (the Output Field Separator) to display an array with different separators:

  &{ $a = 1,2,3; "$a"}
  1 2 3

  &{ $OFS="-"; $a = 1,2,3; "$a"}
  1-2-3

"Democracy cannot be static. Whatever is static is dead" - Eleanor Roosevelt

Related:
Variables - Environment variables
Array Variables



Back to the Top

Simon Sheppard
SS64.com