PowerShell PSScriptRoot为null

kum*_*mar 13 powershell powershell-4.0

当我运行$PSScriptRoot它返回null.我正在使用PS版本4.

$val  = Join-Path -Path $PSScriptRoot WebPlatformInstaller_amd64_en-US.msi
Run Code Online (Sandbox Code Playgroud)

错误

Join-Path:无法将参数绑定到参数'Path',因为它是一个空字符串.

Xop*_*her 12

如果使用ISE使用:

$psISE.CurrentFile.FullPath

启动ISE时,会创建$ psISE并可用于确定ISE实例的当前路径.这是在3.0版中引入的.

请参阅ISE对象模型层次结构

如果你想在shell或ISE中获取路径,你可以使用这样的东西:

if ($psISE)
{
    Split-Path -Path $psISE.CurrentFile.FullPath        
}
else
{
    $global:PSScriptRoot
}
Run Code Online (Sandbox Code Playgroud)


k7s*_*s5a 5

您必须确保此表达式位于已保存的.ps1脚本中.

这可能发生在以下情况:

  • 您在PowerShell ISE控制台中使用此语句
  • 您可以在没有脚本文件的PowerShell控制台中使用此语句
  • 您只标记此表达式以在PowerShell ISE中执行

  • 那么如何在ISE中调试脚本......? (5认同)