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.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
kumar
  • 8,207
  • 20
  • 85
  • 176

3 Answers3

23

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
}
Xopher
  • 339
  • 2
  • 7
  • 1
    $psISE.CurrentFile.FullPath does not show the file path if the file is called from another powershell script – OrigamiEye Jan 24 '22 at 13:52
13

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
Emmanuel DURIN
  • 4,803
  • 2
  • 28
  • 53
k7s5a
  • 1,287
  • 10
  • 18
3

You can use

$PWD.Path 

It works in PowerShell ISE and Powershell Console. It returns the Present Working Directory.

$PSScriptRoot is the Root Path of where the current script is saved. When used in a command it will return blank as there is no script who's current path you are looking for.

OrigamiEye
  • 864
  • 1
  • 12
  • 31