How can I get the current PowerShell executing file?

前端 未结 10 1727
挽巷
挽巷 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条回答
  •  猫巷女王i
    2020-12-07 16:03

    I've tried to summarize the various answers here, updated for PowerShell 5:

    • If you're only using PowerShell 3 or higher, use $PSCommandPath

    • If want compatibility with older versions, insert the shim:

      if ($PSCommandPath -eq $null) { function GetPSCommandPath() { return $MyInvocation.PSCommandPath; } $PSCommandPath = GetPSCommandPath; }

      This adds $PSCommandPath if it doesn't already exist.

      The shim code can be executed anywhere (top-level or inside a function), though $PSCommandPath variable is subject to normal scoping rules (eg, if you put the shim in a function, the variable is scoped to that function only).

    Details

    There's 4 different methods used in various answers, so I wrote this script to demonstrate each (plus $PSCommandPath):

    function PSCommandPath() { return $PSCommandPath; }
    function ScriptName() { return $MyInvocation.ScriptName; }
    function MyCommandName() { return $MyInvocation.MyCommand.Name; }
    function MyCommandDefinition() {
        # Begin of MyCommandDefinition()
        # Note: ouput of this script shows the contents of this function, not the execution result
        return $MyInvocation.MyCommand.Definition;
        # End of MyCommandDefinition()
    }
    function MyInvocationPSCommandPath() { return $MyInvocation.PSCommandPath; }
    
    Write-Host "";
    Write-Host "PSVersion: $($PSVersionTable.PSVersion)";
    Write-Host "";
    Write-Host "`$PSCommandPath:";
    Write-Host " *   Direct: $PSCommandPath";
    Write-Host " * Function: $(ScriptName)";
    Write-Host "";
    Write-Host "`$MyInvocation.ScriptName:";
    Write-Host " *   Direct: $($MyInvocation.ScriptName)";
    Write-Host " * Function: $(ScriptName)";
    Write-Host "";
    Write-Host "`$MyInvocation.MyCommand.Name:";
    Write-Host " *   Direct: $($MyInvocation.MyCommand.Name)";
    Write-Host " * Function: $(MyCommandName)";
    Write-Host "";
    Write-Host "`$MyInvocation.MyCommand.Definition:";
    Write-Host " *   Direct: $($MyInvocation.MyCommand.Definition)";
    Write-Host " * Function: $(MyCommandDefinition)";
    Write-Host "";
    Write-Host "`$MyInvocation.PSCommandPath:";
    Write-Host " *   Direct: $($MyInvocation.PSCommandPath)";
    Write-Host " * Function: $(MyInvocationPSCommandPath)";
    Write-Host "";
    

    Output:

    PS C:\> .\Test\test.ps1
    
    PSVersion: 5.1.19035.1
    
    $PSCommandPath:
     *   Direct: C:\Test\test.ps1
     * Function: C:\Test\test.ps1
    
    $MyInvocation.ScriptName:
     *   Direct:
     * Function: C:\Test\test.ps1
    
    $MyInvocation.MyCommand.Name:
     *   Direct: test.ps1
     * Function: MyCommandName
    
    $MyInvocation.MyCommand.Definition:
     *   Direct: C:\Test\test.ps1
     * Function:
        # Begin of MyCommandDefinition()
        # Note this is the contents of the MyCommandDefinition() function, not the execution results
        return $MyInvocation.MyCommand.Definition;
        # End of MyCommandDefinition()
    
    
    $MyInvocation.PSCommandPath:
     *   Direct:
     * Function: C:\Test\test.ps1
    

    Notes:

    • Executed from C:\, but actual script is C:\Test\test.ps1.
    • No method tells you the passed invocation path (.\Test\test.ps1)
    • $PSCommandPath is the only reliable way, but was introduced in PowerShell 3
    • For versions prior to 3, no single method works both inside and outside of a function

提交回复
热议问题