How can I get the current PowerShell executing file?

前端 未结 10 1724
挽巷
挽巷 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: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)
    

提交回复
热议问题