10

Is there any other way except $MyInvocation.InvocationName in powershell to get the script name? As i need to turn my script in an exe and in that case it doesnt work on that exe.

2 Answers 2

11

I'm assuming since you convert the powershell script to an executable that you are after the location of the executable. You can get it this way:

[Environment]::GetCommandLineArgs()[0]
Sign up to request clarification or add additional context in comments.

2 Comments

fwiw - if you are debugging this in ISE, the script name becomes the second parameter: [Environment]::GetCommandLineArgs()[1]
Try $PSCommandPath or $MyInvocation.PSCommandPath (according to this great answer : stackoverflow.com/a/43643346/5649639)
3

If you want something that works within and outside of ISE you can use

$MyInvocation.InvocationName

Since full paths and .\YourScript.ps1 can be returned you can parse the name with:

[Regex]::Match( $MyInvocation.InvocationName, '[^\\]+\Z', [System.Text.RegularExpressions.RegexOptions]::IgnoreCase -bor [System.Text.RegularExpressions.RegexOptions]::SingleLine ).Value

1 Comment

When called from within a function, $MyInvocation.InvocationName is the function's name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.