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
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]
2 Comments
Dave Wise
fwiw - if you are debugging this in ISE, the script name becomes the second parameter:
[Environment]::GetCommandLineArgs()[1]SebMa
Try
$PSCommandPath or $MyInvocation.PSCommandPath (according to this great answer : stackoverflow.com/a/43643346/5649639)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
Laurent CAPRANI
When called from within a function,
$MyInvocation.InvocationName is the function's name.