M HYPE SPLASH
// general

Get current script file

By Emily Wilson

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.

1

1 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.Name

Returns 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.ScriptName

this 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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy