Getting the Script Name in Powershell

Is there any other way besides $ MyInvocation.InvocationName in powershell to get the script name? Since I need to turn my script into exe, in which case it will not work on this exe either.

+6
source share
2 answers

I assume that you are converting the powershell script into an executable file that you after the location of the executable file. This can be done like this:

[Environment]::GetCommandLineArgs()[0] 
+11
source

If you want something that works inside and outside ISE, you can use

 $MyInvocation.InvocationName 

Since the full path 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 
0
source

Source: https://habr.com/ru/post/907386/


All Articles