首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何获取当前的PowerShell执行文件?

如何获取当前的PowerShell执行文件?
EN

Stack Overflow用户
提问于 2009-05-03 14:27:10
回答 10查看 158.2K关注 0票数 114

注意: PowerShell 1.0

我想要得到当前正在执行的PowerShell文件名。也就是说,如果我像这样开始我的会话:

powershell.exe .\myfile.ps1

我想得到字符串".\myfile.ps1" (或类似的东西)。编辑"myfile.ps1"更可取。

有什么想法吗?

EN

回答 10

Stack Overflow用户

回答已采纳

发布于 2017-04-27 03:50:58

我已经在这里总结了各种答案,并针对PowerShell 5进行了更新:

如果您只使用PowerShell 3或更高版本,请使用$PSCommandPath

  • 如果要与旧版本兼容,请插入填充程序:

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

这将添加$PSCommandPath (如果它不存在)。

填充代码可以在任何地方(顶级或函数内部)执行,尽管$PSCommandPath变量受正常作用域规则的约束(例如,如果将填充程序放入函数中,则该变量的作用域仅限于该函数)。

详细信息

在不同的答案中有4种不同的方法,所以我写了这个脚本来演示每种方法(加上$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: $(PSCommandPath)";
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 "";

输出:

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

备注:

C:\Test\test.ps1.

  • No方法告诉您传递的调用路径(.\Test\test.ps1)

  • $PSCommandPath是唯一可靠的方法,但它是在PowerShell 3

  • 3之前的版本中引入的,没有一种方法可以在

的内部和外部同时使用

票数 96
EN

Stack Overflow用户

发布于 2009-05-03 21:39:13

如果您只需要文件名(而不是完整路径),请使用以下命令:

$ScriptName = $MyInvocation.MyCommand.Name
票数 74
EN

Stack Overflow用户

发布于 2009-05-03 14:29:33

请尝试以下操作

$path =  $MyInvocation.MyCommand.Definition 

这可能不会为您提供键入的实际路径,但它会为您提供文件的有效路径。

票数 34
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/817198

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档