Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I import a Powershell cmdlet globally from a function in another module?

Tags:

powershell

I'm hitting a problem importing cmdlet globally from a function in another module.

Here is the case. Say I'm trying to call Test-Method in an dll. When I run Import-Module <dll path>, things work normally and I can call the Test-Method with no problem.

Then I put the command into a function to simplify usage. Now I have something like:

function Import-Cmdlets
{
    Import-Module "<dll path>" -Scope Global -Force
}

Now in Powershell I call Import-Cmdlets, and then I can call Test-Method with no problem.

However, when I put the function into a psm1 file, and import module on the psm1 file, I cannot find Test-Method anymore.

Now I have a mymodule.psm1 file with following content:

function Import-Cmdlets
{
    Import-Module "<dll path>" -Scope Global -Force
}

Then in PowerShell I run:

Import-Module mymodule.psm1 -Force
Import-Cmdlets

Now I cannot find Test-Method any more. The dll shows up when I run Get-Module and I can see Test-Method from ExportedCommands. But I cannot access it.

This only happens for dll imports. I’ve tried to use a psm1 file to replace the dll path, and didn’t meet this issue.

What is a good work around or solution for this problem?

like image 620
Jun Pang Avatar asked Nov 09 '22 14:11

Jun Pang


1 Answers

I had the same problem and adding the parameter -Scope Global fixed it

Change your line to Import-Module <path to your module> **-Scope Global**

like image 91
Pierre Goch Avatar answered Nov 14 '22 22:11

Pierre Goch