9

When I use

Import-Module -Name <path_to_local_dll> -Verbose

the cmdlets contained in the DLL file are not exported.

Thus, when I type Get-Module my imported module is listed, but without any ExportedCommands. Why?

ModuleType Name                      ExportedCommands
---------- ----                      ----------------
Binary     MyModule

On a second PC with the same software (PowerShell, .NET Framework, ...), the same imported DLL file works fine. There I get ExportedCommands.

On what can this behaviour depend?

Unfortunately, the Import-Module cmdlet gives no indication that it failed to import the cmdlets. Is there a way to get an indication why it fails?

5
  • What version of .NET did you compile against and what version of System.Management.Automation.dll (1.0 or 3.0) does it reference? Also what version of PowerShell (and bitness) is the first machine running? Is your assembly compiled Any CPU? Commented Oct 12, 2015 at 14:28
  • I didn't compiled the dll. I just use it. Thus, I don't know the version of .NET it was compiled against and the version of System.Management.Automation.dll. I use 32 bit PowerShell 4.0. (PSVersion 4.0, WSManStackVersion 3.0, SerializationVersion 1.1.0.1, CLRVersion 4.0.30319.34209, BuildVersion 6.3.9600.16406, PSCompatibleVersions {1.0, 2.0, 3.0, 4.0}, PSRemotingProtocolVersion 2.2) Commented Oct 13, 2015 at 11:24
  • Have you figured out the root cause of this problem? I'm having similar issue. Please let me know if you have a solution/workaround. Thanks ! Commented May 20, 2016 at 17:46
  • No, I didn't. As I had further problems based on this, I had to re-installed windows and all other stuff. Commented May 30, 2016 at 11:24
  • I was using a third-party tool (PS Protector) to convert *.psm1 to *.dll when I encountered this issue, despite having the *.psd1 file configured correctly. I actually was unable to locate the precise issue - it turned out that the problem was with the dll file, and generating the EXACT SAME dll with a slightly different name fixed the problem. Bizarre. Commented Jul 29, 2021 at 7:49

4 Answers 4

13

Two things:

  1. Make sure you're using a module manifest file (.psd1 file). More information can be found in How to Write a Module Manifest

  2. Most importantly, edit your manifest file and make sure it references your root module as follows:

    RootModule = 'name of your module'

I just finished fighting with this for a few hours and I couldn't figure out what I was missing from my other modules. This did the trick for sure!

Sign up to request clarification or add additional context in comments.

2 Comments

Is 'name of your module' with our without the ".psm1" extension? What does this power?
Without the .psm1. I just use the same module name. I created one called "CxdCallData" my manifest looks like: RootModule = "CxdCallData".
2

One other requirement: ensure that the cmdlet class is public. For example, in my .cs file I initially had:

[Cmdlet(VerbsCommon.Get, "Proc")]
class GetProcCommand : Cmdlet
{ ...

Even after adding a manifest file with RootModule set, Get-Module continued to show no ExportedCommands after my Import-Module. To fix it I just marked the class as public and rebuilt my .dll assembly:

[Cmdlet(VerbsCommon.Get, "Proc")]
public class GetProcCommand : Cmdlet
{ ...

I figured this out while examining my .dll using ildasm - I noticed that some of my classes were public, but my cmdlet class was private.

1 Comment

The constructor needs to be public, as well
0

It may be that the psd1 file (the module manifest) does not contain the commands.

This page has a tutorial on how to create a module manifest.

Comments

0

Explicitly exporting function from the PowerShell module worked for me:

function New-CmdLetNameHere
{
    ...
}
Export-ModuleMember -Function New-CmdLetNameHere

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.