Get current script file
Within a PS script, how can I retrieve an object that represents or points to the script file? Something like get-currentscript.
What I really want to do is retrieve the script file creation and/or modification time. Presumably this would be retrievable from the script file object. Of course, I could also use a dumb old file spec for the current script file.
11 Answer
Check out this answer on StackOverflow:
While the current Answer is right in most cases, there are certain situations that it will not give you the correct answer. If you use inside your script functions then:
$MyInvocation.MyCommand.NameReturns the name of the function instead name of the name of the script.
function test { $MyInvocation.MyCommand.Name
}Will give you "test" no matter how your script is named. The right command for getting the script name is always
$MyInvocation.ScriptNamethis returns the full path of the script you are executing. If you need just the script filename than this code should help you:
Split-Path $MyInvocation.PSCommandPath -Leaf 0