'What is the equivalent of $PSScriptRoot in Powershell ISE?
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
Solution 1:[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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | OrigamiEye |
