How can I get the current PowerShell executing file?

前端 未结 10 1707
挽巷
挽巷 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 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
    0 讨论(0)
  • 2020-12-07 16:07

    Did some testing with the following script, on both PS 2 and PS 4 and had the same result. I hope this helps people.

    $PSVersionTable.PSVersion
    function PSscript {
      $PSscript = Get-Item $MyInvocation.ScriptName
      Return $PSscript
    }
    ""
    $PSscript = PSscript
    $PSscript.FullName
    $PSscript.Name
    $PSscript.BaseName
    $PSscript.Extension
    $PSscript.DirectoryName
    
    ""
    $PSscript = Get-Item $MyInvocation.InvocationName
    $PSscript.FullName
    $PSscript.Name
    $PSscript.BaseName
    $PSscript.Extension
    $PSscript.DirectoryName
    

    Results -

    Major  Minor  Build  Revision
    -----  -----  -----  --------
    4      0      -1     -1      
    
    C:\PSscripts\Untitled1.ps1
    Untitled1.ps1
    Untitled1
    .ps1
    C:\PSscripts
    
    C:\PSscripts\Untitled1.ps1
    Untitled1.ps1
    Untitled1
    .ps1
    C:\PSscripts
    
    0 讨论(0)
  • 2020-12-07 16:07

    This can works on most powershell versions:

    (& { $MyInvocation.ScriptName; })
    

    This can work for Scheduled Job

    Get-ScheduledJob |? Name -Match 'JOBNAMETAG' |% Command
    
    0 讨论(0)
  • 2020-12-07 16:11

    beware: Unlike the $PSScriptRoot and $PSCommandPath automatic variables, the PSScriptRoot and PSCommandPath properties of the $MyInvocation automatic variable contain information about the invoker or calling script, not the current script.

    e.g.

    PS C:\Users\S_ms\OneDrive\Documents> C:\Users\SP_ms\OneDrive\Documents\DPM ...
    =!C:\Users\S_ms\OneDrive\Documents\DPM.ps1
    

    ...where DPM.ps1 contains

    Write-Host ("="+($MyInvocation.PSCommandPath)+"!"+$PSCommandPath)
    
    0 讨论(0)
提交回复
热议问题