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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
H. Seeger
  • 91
  • 1
  • 1
  • 4
  • 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? – Keith Hill Oct 12 '15 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) – H. Seeger Oct 13 '15 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 ! – Hasan May 20 '16 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. – H. Seeger May 30 '16 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. – Steve can help Jul 29 '21 at 07:49

4 Answers4

12

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!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jason Shave
  • 2,462
  • 2
  • 29
  • 47
1

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.

pjhsea
  • 841
  • 9
  • 13
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.

bgmCoder
  • 6,205
  • 8
  • 58
  • 105
0

Explicitly exporting function from the PowerShell module worked for me:

function New-CmdLetNameHere
{
    ...
}
Export-ModuleMember -Function New-CmdLetNameHere
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Deep
  • 5,772
  • 2
  • 26
  • 36