PowerShell PSScriptRoot is null

kumar picture kumar · Jun 10, 2017 · Viewed 15.7k times · Source

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.

Answer

Xopher picture Xopher · Aug 22, 2018

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
}