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
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