Search notes:

PowerShell: The automatic variable $MyInvocation

The automatic variable $myInvocation stores information about the current command if the command is a function, a script or a script block.

Type of $myInvocation

The .NET framework type of $myInvocation is System.Management.Automation.InvocationInfo as can be demonstrated with
PS C:\> $myInvocation.getType().fullName

Properties of $myInvocation / InvocationInfo

Property Comment Misc
MyCommand An object that inherits from System.Management.Automation.CommandInfo object. In the case of a script, it's a System.Management.Automation.CommandInfo or System.Management.Automation.ExternalScriptInfo object and MyCommand.Name evaluates to the filename of the current script, MyCommand.Path to its directory. See also determining the name and directory of a PowerShell script within the script itself.
BoundParameters a System.Management.Automation.PSBoundParametersDictionary object that stores the parameters that were bound for this script or command.
UnboundArguments a System.Collections.Generic.List<System.Object> object that lists the arguments that were not bound to any parameter.
ScriptLineNumber The line in the invoking script that contains the command being invoked
OffsetInLine
HistoryId
ScriptName The name of the script that contains the cmdLet What's the difference to PSCommandPath
Line The text (of line ScriptLineNumber) that caused the invocation of the current command
PositionMessage
PSScriptRoot The absolute path of the script that invoked the current command Compare with the automatic variable $psScriptRoot which stores the absolute path of the script being invoked.
PSCommandPath The absolute path to the directory where where the script is located that invoked the current command Compare with the automatic variable $psCommandPath which stores the absolute path of the directory in which the script being invoked is located. What's the difference to ScriptName?
InvocationName
PipelineLength
PipelinePosition
ExpectingInput
CommandOrigin A System.Management.Automation.CommandOrigin enum, either Runspace or Internal.
DisplayScriptPosition

Determining the name of a script

Arguably, one of the most interesting uses of $myInvocation is that it allows to determine the the name of a script within the script:
$scriptName = $myInvocation.myCommand.name
$myInvocation.myCommand.name always evaluates to a file name only, without any path components added to it, regardless if the script was invoked with a relative or absolute path name or from a different directory.
In order to get the full and absolute path name of a script, the expression $myInvocation.myCommand.path must be used.
Outside of a script, the expression $myInvocation.myCommand.path is $null.

Determining the directory in which a script is located

Another interesting case of $myInvocation is that it allows to determine the directory in which an script is located.
myInvocation.myCommand.path contains the full path (directory and script file name), therefore, in order to obtain the directory, the value of $myInvocation.mycommand.path must be passed to split-path:
$dir = split-path $myInvocation.myCommand.path
Compare this value with that of $psScriptRoot

Script to demonstrate some values of myInvocation

The following script is intended to display some values the properties of $myInvocation. The script body calls the function func-1 which calls the function func-2.
The script body as well as the functions print the (imho) interesting values:
function func-2 {

#  write-host "`$myInvocation.GetType().FullName           = $($myinvocation.GetType().FullName)"
   write-host "`$myInvocation.myCommand.GetType().FullName = $($myinvocation.myCommand.GetType().FullName)"
   write-host "`$myInvocation.myCommand.name               = $($myinvocation.myCommand.name)"
#  write-host "`$myInvocation.myCommand.path               = $($myinvocation.myCommand.path)"
   write-host "`$myInvocation.scriptName                   = $($myinvocation.scriptName)"
   write-host "`$myInvocation.psScriptRoot                 = $($myinvocation.psScriptRoot)"
   write-host "`$myInvocation.psCommandPath                = $($myinvocation.psCommandPath)"
   write-host "`$myInvocation.commandOrigin                = $($myinvocation.commandOrigin)"

}

function func-1 {

#  write-host "`$myInvocation.GetType().FullName           = $($myinvocation.GetType().FullName)"
   write-host "`$myInvocation.myCommand.GetType().FullName = $($myinvocation.myCommand.GetType().FullName)"
   write-host "`$myInvocation.myCommand.name               = $($myinvocation.myCommand.name)"
#  write-host "`$myInvocation.myCommand.path               = $($myinvocation.myCommand.path)"
   write-host "`$myInvocation.scriptName                   = $($myinvocation.scriptName)"
   write-host "`$myInvocation.psScriptRoot                 = $($myinvocation.psScriptRoot)"
   write-host "`$myInvocation.psCommandPath                = $($myinvocation.psCommandPath)"
   write-host "`$myInvocation.commandOrigin                = $($myinvocation.commandOrigin)"

   write-host

   func-2

}

#  write-host "`$myInvocation.GetType().FullName           = $($myinvocation.GetType().FullName)"
   write-host "`$myInvocation.myCommand.GetType().FullName = $($myinvocation.myCommand.GetType().FullName)"
   write-host "`$myInvocation.myCommand.name               = $($myinvocation.myCommand.name)"
   write-host "`$myInvocation.myCommand.path               = $($myinvocation.myCommand.path)"
   write-host "`$myInvocation.scriptName                   = $($myinvocation.scriptName)"
   write-host "`$myInvocation.psScriptRoot                 = $($myinvocation.psScriptRoot)"
   write-host "`$myInvocation.psCommandPath                = $($myinvocation.psCommandPath)"
   write-host "`$myInvocation.commandOrigin                = $($myinvocation.commandOrigin)"

write-host

func-1
Github repository about-PowerShell, path: /language/variable/automatic/myInvocation/script.ps1

See also

MyInvocation is also a member of the System.Management.Automation.PSScriptCmdlet class, whose instance for the current cmdLet is exposed in the $psCmdLet automatic variable.
Other automatic variables

Index