How to `Import-Module` in PowerShell to parent sessionprocess? - Stack Overflow

admin2025-04-17  18

I store imports for my favourite modules in a myFavMods.ps1:

Import-Module a
Import-Module b
...

When running it like this the modules are not available in my session:

> ./myFavMods.ps1
> funFromModuleA
funFromModuleA: The term 'funFromModuleA' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

When running it using the . operator it works:

> . ./myFavMods.ps1
> funFromModuleA
Hurray this is funFromModuleA reporting!

Is it possible to have funFromModuleA available regardless of how I execute my script? Is it possible to Import-Module -Scope 'GlobalGlobalAllTheWayUp'?

What I know so far

Profile

I am aware of PowerShell profiles but I would like my favourite modules only available when imported explicitly.

Make the script inspect how it's being executed

I could use $MyInvocation.Line to check if the script is being invoke with a . but I would rather have it work always regardless of the way of invocation.

Import-Module -Scope

This can only be used to narrow down the scope to Local.

I store imports for my favourite modules in a myFavMods.ps1:

Import-Module a
Import-Module b
...

When running it like this the modules are not available in my session:

> ./myFavMods.ps1
> funFromModuleA
funFromModuleA: The term 'funFromModuleA' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

When running it using the . operator it works:

> . ./myFavMods.ps1
> funFromModuleA
Hurray this is funFromModuleA reporting!

Is it possible to have funFromModuleA available regardless of how I execute my script? Is it possible to Import-Module -Scope 'GlobalGlobalAllTheWayUp'?

What I know so far

Profile

I am aware of PowerShell profiles but I would like my favourite modules only available when imported explicitly.

Make the script inspect how it's being executed

I could use $MyInvocation.Line to check if the script is being invoke with a . but I would rather have it work always regardless of the way of invocation.

Import-Module -Scope

This can only be used to narrow down the scope to Local.

Share Improve this question asked Feb 1 at 12:35 inwenisinwenis 4078 silver badges26 bronze badges 1
  • Add funFromModuleA to the list of exported commands in the module manifest for module A, let PowerShell's module autoloading facility take care of the rest – Mathias R. Jessen Commented Feb 1 at 14:14
Add a comment  | 

1 Answer 1

Reset to default 2

For this you might consider to create a module manifest that includes all your "funFrom" modules:

.\myFavMods\a.psm1

function funFromModuleA { 'Fun from module A.'}
Export-ModuleMember -Function funFromModuleA

.\myFavMods\a.psm1

function funFromModuleB { 'Fun from module B.'}
Export-ModuleMember -Function funFromModuleB

New manifest

The following command creates a new module manifest at .\myFavMods\myFavMods.psd1 that exposes the conserned nested modules:

New-ModuleManifest .\myFavMods\myFavMods.psd1  -NestedModules '.\a.psm1', '.\b.psm1'

.\myFavMods\myFavMods.ps1

In case you want to invoke the "funFrom" module from a PowerShell Script (.ps1 file):

Import-Module $PSScriptRoot\..\myFavMods -Global

Test

.\myFavMods\myFavMods.ps1
funFromModuleA
Fun from module A.
转载请注明原文地址:http://anycun.com/QandA/1744828249a88182.html