Get script import path from module?

Is there any way to get the path to the script that imported the module from this module?

The script module that I am writing is designed to load settings from files regarding import script. I plan to reuse the module for a number of projects, so I would prefer that the module cannot make any assumptions about where to import it from.

It's nice to have, it would be great that the module can be as explicit as possible. If all else fails, I can simply transfer the call to my location.

Unfortunately, everything I tried so far returns the path to the module (and not the one that imported it). Here is a simple demo:


Test-RelativeModule.ps1 , saved to: c: \ test \

import-module "$PSScriptRoot\mod\Test.psm1"

Test.psm1 , saved to: c: \ test \ mod \

# returns 'c:\test\mod'
write-host "`$PSScriptRoot: $PSScriptRoot"

# returns 'c:\test\mod'
# The value of $MyInvocation.MyCommand.Path is 'c:\test\mod\Test.psm1'
write-host "Split Invoation: $(Split-Path $MyInvocation.MyCommand.Path)"

# returns whatever path the console is currently residing
write-host "Resolve Path: $((resolve-path '.\').path)"

# what I'm looking for is something to return 'c:\test' from within the module
# without making any assumptions about the folder structure
+4
1

:

Write-Host "My invoker PSScriptRoot: $($MyInvocation.PSScriptRoot)"
+4

All Articles