Press "Enter" to skip to content

Month: October 2016

TIL – October 22 2016

Been a few days of learning since I last wrote one of these, but I have come back to the automatic variables page on the PowerShell documentation enough times that I think I should just blog the important parts for myself.

  • $? – TRUE/FALSE if the last thing you did succeeded.
  • $_ – Something everyone uses in posh, current pipeline object.
  • $Args – all the undeclared params passed to a function, try to avoid.
  • $Error – the array of error objects that represent a stack of the most recent errors. use $Errors[0] to get the most recent error.
  • $Home – full path of home directory of the current user.
  • $Host – methods to access information about the host we are running the PowerShell command on.
  • $Matches – used with the -(not)match operators, is populated with a hash table of the matched values.
  • $MyInvocation – name, params, values, and information about the current command. Super useful for weird introspection.
  • $OFS – Output File Separator, allows you to set the default file separator for array operations. Generally better to set variables to send to split, this can have downstream effects if you dont change it back.
  • $PID – PID of the process hosting the current PowerShell process.
  • $PSBoundParameters – Nice shortcut to refer to all the params to another script or function.
  • $PsCmdlet – mostly used in advanced functions to access the current running cmdlet’s properties.
  • $PSCommandPath – full path and file name of the script being run.
  • $PSDebugContext – if $null, debugger is not running.
  • $PsHome – path to PowerShell install dir.
  • $PSScriptRoot – directory where script is being run from, avoid if you want to support ps2.
  • $PsVersionTable – returns read only hash table about the current ps session.
  • $Pwd – a path object representing the current directory.
  • $StackTrace – shortcut for the stack trace of the most recent error.
  • $This – refers to an object that is being extended in a script property or method.
  • $true/$false/$null – use boolean login on all of these, nothing new here.

Read (slightly) more at about_Automatic_Variables