I'm looking for a way to get the path of the main (lets say 'entry point') PowerShell script (*.ps1) file.
Let's for example consider the following example:
- I have the following files:
"C:\MyDir1\Main.ps1" calls a function in
"C:\MyDir2\SubDir1\MyModule.psm1" which calls a function in
"C:\AnotherDir\AnotherScript.ps1"
- I run the following PowerShell command:
"PowerShell.exe -File "C:\MyDir1\Main.ps1"
- What I want: from the script "AnotherScript.ps1", I want to find the path to "C:\MyDir1\Main.ps1".
Note 1: Please note that I'm not talking about the path of the current script which is available via $PSScriptRoot variable.
Note 2: It appears that the variable $global:PSScriptRoot (not the same as $PSScriptRoot) is what I'm looking for but unfortunately, this value is empty when the main script is invoked from a PowerShell session.
@(Get-PSCallStack)[1].ScriptName
or@(Get-PSCallStack)[1].Location
– Theo@(Get-PSCallStack)[-1].ScriptName
do the trick? – Theo