PowerShell PSScriptRoot is null

喜夏-厌秋 提交于 2019-12-06 18:39:24

问题


When I run $PSScriptRoot it returns null. I am using PS version 4.

$val  = Join-Path -Path $PSScriptRoot WebPlatformInstaller_amd64_en-US.msi

Error

Join-Path : Cannot bind argument to parameter 'Path' because it is an empty string.


回答1:


If using ISE use:

$psISE.CurrentFile.FullPath

When ISE is launched, $psISE is created and can be used to determine the current path of the ISE instance. This was introduced in version 3.0.

See ISE Object Model Hierarchy

If you wanted to get the path in either shell or ISE you could use something like this:

if ($psISE)
{
    Split-Path -Path $psISE.CurrentFile.FullPath        
}
else
{
    $global:PSScriptRoot
}



回答2:


You have to make sure that this expression is in a saved .ps1 script.

This can happened in following cases:

  • You use this statement in PowerShell ISE console
  • You use this statement in PowerShell console without a script file
  • You marked only this expression for execution in PowerShell ISE


来源:https://stackoverflow.com/questions/44474074/powershell-psscriptroot-is-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!