25

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
  • 2
    Do you execute this in PowerShell ISE? Commented Jun 10, 2017 at 13:54

3 Answers 3

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
}
Sign up to request clarification or add additional context in comments.

1 Comment

$psISE.CurrentFile.FullPath does not show the file path if the file is called from another powershell script
15

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

3 Comments

Then how to debug the script in ISE...?
I guess it can't be set while debugging either as the value is ignored, then heads up if you change the script it will be forcefully saved in order to debug.
Wow, I feel like I might file a bug for this. I have a script on linux without an extension, and use #!/bin/pwsh at the top to execute the file. Without the extension $PSScriptRoot is empty, but if I add it it works. I don't see a reason for this.
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.

1 Comment

Works in Windows PowerShell and PowerShell Core.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.