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