0

I am working on some scripts in Powershell ISE and I need PS script root to behave the same in Powershell and Powershell ISE. I made the following example to show the difference.

Caller.ps1

if ($psISE)
{
    $directory = Split-Path -Path $psISE.CurrentFile.FullPath 
    Write-Host "psISE : " $directory       
}
else
{
     $directory=$PSScriptRoot
     Write-Host "not psISE : " $directory  
}

write-host "---------- in dir Scripts --------------" 

try {& "$directory\Scripts\Called.ps1"}
catch {"FAILED"}

Called.ps1

if ($psISE)
{
    $directory = Split-Path -Path $psISE.CurrentFile.FullPath 
    Write-Host "psISE : " $directory       
}
else
{
     $directory=$PSScriptRoot
     Write-Host "not psISE : " $directory
}

Results from Powershell

PS C:\> .\Example\Caller.ps1
not psISE :  C:\Example
---------- in dir Scripts --------------
not psISE :  C:\Example\Scripts

Results from Powershell ISE

PS C:\> C:\Example\Caller.ps1
psISE :  C:\Example
---------- in dir Scripts --------------
psISE :  C:\Example

Example first posted in and related question: PowerShell PSScriptRoot is null

OrigamiEye
  • 864
  • 1
  • 12
  • 31
  • 1
    `$PSScriptRoot` should work the same way be it powershellCLI or ISE. And you might want to consider not using ISE anymore and moving to VS Code with the PS Extension. – Santiago Squarzon Jan 24 '22 at 14:06
  • `$PSScriptRoot` has been working in ISE since PowerShell 3.0. Are you using ISE on a vanilla Windows 7? Or are you perhaps executing _a selection_ in ISE, rather than invoking a script? – Mathias R. Jessen Jan 24 '22 at 14:12
  • @MathiasR.Jessen Windows 10 Enterprise, I run it by pressing F5. $PSScriptRoot returns blank. – OrigamiEye Jan 24 '22 at 14:21
  • 1
    @OrigamiEye change `if($psISE)` to `if($psISE -and -not $PSScriptRoot)` – Mathias R. Jessen Jan 24 '22 at 14:24
  • @MathiasR.Jessen It works, can you add some words on how – OrigamiEye Jan 24 '22 at 14:31
  • 2
    `$psISE` is always available regardless of whether the script was invoked directly via F5, or called by a script that was - by checking whether the runtime already assigned a value to `$PSScriptRoot`, you ensure that you only override it when it's the former (eg. the current script is the one launched with F5) – Mathias R. Jessen Jan 24 '22 at 14:39
  • @MathiasR.Jessen It is now calling $PSScriptRoot, which only returns a result when the Script is saved. Thanks I now see it works as expected. – OrigamiEye Jan 24 '22 at 14:53
  • @OrigamiEye it works when you invoke it from the shell (the bottom pane in ISE) or from another script, is does _not_ work when you use `F5`/`F8` :) – Mathias R. Jessen Jan 24 '22 at 14:56
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/241358/discussion-between-origamieye-and-mathias-r-jessen). – OrigamiEye Jan 24 '22 at 14:59

1 Answers1

1

$PSScriptRoot works in PowerShell ISE as long as the file is saved to disk. The file can be run with F5, though not F8.

The problem the example code demonstrates is that psISE.CurrentFile.FullPath does not update from the Called.ps1 file.

OrigamiEye
  • 864
  • 1
  • 12
  • 31
  • Thanks - the fact that it works with F5 but not F8 got me - it was driving me mad as was not working when I did F8. – Ashok Jingar May 24 '23 at 11:27