How can I get the current PowerShell executing file?

前端 未结 10 1706
挽巷
挽巷 2020-12-07 15:04

Note: PowerShell 1.0
I\'d like to get the current executing PowerShell file name. That is, if I start my session like this:

powershell.exe .\\myfile.ps         


        
相关标签:
10条回答
  • 2020-12-07 15:45

    If you are looking for the current directory in which the script is being executed, you can try this one:

    $fullPathIncFileName = $MyInvocation.MyCommand.Definition
    $currentScriptName = $MyInvocation.MyCommand.Name
    $currentExecutingPath = $fullPathIncFileName.Replace($currentScriptName, "")
    
    Write-Host $currentExecutingPath
    
    0 讨论(0)
  • 2020-12-07 15:46

    If you only want the filename (not the full path) use this:

    $ScriptName = $MyInvocation.MyCommand.Name
    
    0 讨论(0)
  • 2020-12-07 15:50

    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 讨论(0)
  • 2020-12-07 15:55

    Try the following

    $path =  $MyInvocation.MyCommand.Definition 
    

    This may not give you the actual path typed in but it will give you a valid path to the file.

    0 讨论(0)
  • 2020-12-07 16:01

    I would argue that there is a better method, by setting the scope of the variable $MyInvocation.MyCommand.Path:

    ex> $script:MyInvocation.MyCommand.Name

    This method works in all circumstances of invocation:

    EX: Somescript.ps1

    function printme () {
        "In function:"
        ( "MyInvocation.ScriptName: " + [string]($MyInvocation.ScriptName) )
        ( "script:MyInvocation.MyCommand.Name: " + [string]($script:MyInvocation.MyCommand.Name) )
        ( "MyInvocation.MyCommand.Name: " + [string]($MyInvocation.MyCommand.Name) )
    }
    "Main:"
    ( "MyInvocation.ScriptName: " + [string]($MyInvocation.ScriptName) )
    ( "script:MyInvocation.MyCommand.Name: " + [string]($script:MyInvocation.MyCommand.Name) )
    ( "MyInvocation.MyCommand.Name: " + [string]($MyInvocation.MyCommand.Name) )
    " "
    printme
    exit
    

    OUTPUT:

    PS> powershell C:\temp\test.ps1
    Main:
    MyInvocation.ScriptName:
    script:MyInvocation.MyCommand.Name: test.ps1
    MyInvocation.MyCommand.Name: test.ps1
    
    In function:
    MyInvocation.ScriptName: C:\temp\test.ps1
    script:MyInvocation.MyCommand.Name: test.ps1
    MyInvocation.MyCommand.Name: printme
    

    Notice how the above accepted answer does NOT return a value when called from Main. Also, note that the above accepted answer returns the full path when the question requested the script name only. The scoped variable works in all places.

    Also, if you did want the full path, then you would just call:

    $script:MyInvocation.MyCommand.Path
    
    0 讨论(0)
  • 2020-12-07 16:02

    As noted in previous responses, using "$MyInvocation" is subject to scoping issues and doesn't necessarily provide consistent data (return value vs. direct access value). I've found that the "cleanest" (most consistent) method for getting script info like script path, name, parms, command line, etc. regardless of scope (in main or subsequent/nested function calls) is to use "Get-Variable" on "MyInvocation"...

    # Get the MyInvocation variable at script level
    # Can be done anywhere within a script
    $ScriptInvocation = (Get-Variable MyInvocation -Scope Script).Value
    
    # Get the full path to the script
    $ScriptPath = $ScriptInvocation.MyCommand.Path
    
    # Get the directory of the script
    $ScriptDirectory = Split-Path $ScriptPath
    
    # Get the script name
    # Yes, could get via Split-Path, but this is "simpler" since this is the default return value
    $ScriptName = $ScriptInvocation.MyCommand.Name
    
    # Get the invocation path (relative to $PWD)
    # @GregMac, this addresses your second point
    $InvocationPath = ScriptInvocation.InvocationName
    

    So, you can get the same info as $PSCommandPath, but a whole lot more in the deal. Not sure, but it looks like "Get-Variable" was not available until PS3 so not a lot of help for really old (not updated) systems.

    There are also some interesting aspects when using "-Scope" as you can backtrack to get the names, etc. of the calling function(s). 0=current, 1=parent, etc.

    Hope this is somewhat helpful.

    Ref, https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/get-variable

    0 讨论(0)
提交回复
热议问题