4

I am using Microsoft PowerShell v4:

PS C:\> get-host

Name             : ConsoleHost
Version          : 4.0
InstanceId       : 3b4b6b8d-70ec-46dd-942a-bfecf5fb6f31
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : de-CH
CurrentUICulture : en-US
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace

I have developed a C# project in Visual Studio 2012 targeting .NET Framework 4 which contains some Cmdlet and the Snapin. I can debug them and everything works just fine.

I've created the path C:\PowerShell\Modules\ and added it to the PSModulePath environment variable.

I put the rMySnapIn.dll to the path C:\PowerShell\Modules\MySnapIn.

I would expect that the module is automatically loaded so I have my new cmdlets ready to use, but they're not: the module is not loaded. I have to write Import-Module MySnapin in order to get it loaded.

How can I get the module automatically loaded?

JoanComasFdz
  • 2,911
  • 5
  • 34
  • 50

4 Answers4

17

A checklist that may help you identify the issue:

  1. According to What's New in Windows PowerShell, "Automatic importing of modules is triggered by (a) using the cmdlet in a command, (b) running Get-Command for a cmdlet without wildcards, or (C) running Get-Help for a cmdlet without wildcards." (That applies to V3 and V4.) How did you confirm the module was not loaded?

  2. According to about_Modules, "Only modules that are stored in the location specified by the PSModulePath environment variable are automatically imported." You stated that you did add your path to PSModulePath. When I examine mine, I see that each path included is terminated with a backslash, so in your case you would need C:\PowerShell\Modules\ rather than just C:\PowerShell\Modules. What is the value of your $env:PsModulePath ?

  3. According to this post from Thomas Lee as well as my own experience, autoloading does not work with script modules; however, you state you are using a compiled module, so this should not be your issue.

  4. The $PSModuleAutoLoadingPreference preference variable can be used to turn off autoloading; however, unless you have explicitly changed it, it defaults to All so likely that is not the problem (about_Preference_Variables shows you the possible values). What is your value of $PSModuleAutoLoadingPreference ?

  5. Last but not least--I am particularly suspicious over the fact that you seem to be mixing snapins and modules. They are distinct types of entities, and are not designed to be mixed. Snapins are loaded via Add-PSSnapin. Modules are loaded via Import-Module. And modules, as you know, are also loaded by auto-loading--I suspect that may not be true of code written as a snapin. Furthermore, snapins are deprecated; new code should be written using modules (that is, derive from Cmdlet or PSCmdlet, as detailed in Writing a Windows PowerShell Cmdlet).

Michael Sorens
  • 35,361
  • 26
  • 116
  • 172
  • Thanks for the answer: 1) I confirmed it using get-module, it wasn't listed. 2) Yes, I double checked it, my path ends with a backslash. 3) Thanks anyway. 4) $PSModuleAutoLoadingPreference has no value when I query it. 5) I also think that I am mixing concepts. I have some classes inheriting from PSCmdlet and also a SnapIn class in my C# project. – JoanComasFdz Jun 02 '14 at 05:34
  • You can auto-load script modules - at least it works for me in PS 4 and 5, maybe it didn't work in older versions. – Cary Nov 10 '15 at 00:43
  • I have tried all of the above, and I still just get the first of nested modules autoloaded – Klas Mellbourn Jan 25 '16 at 13:40
  • I have two user accounts on a server. On one, the backslash missing from the $env:PSMODULEPATH was the cause of autoload not working. However on the other user account, even this was not enough to fix it. – Cody Konior Feb 20 '17 at 10:38
5

If you want to load it automatically you can add the Import-Module MySnapin command line to your PowerShell profile.

To find out the location of your PowerShell profile just type $profile in a PowerShell and by default the profile path is:

C:\Documents and Settings\User\My Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

If the Microsoft.PowerShell_profile.ps1 file does not exist just create it.

Iulian Onofrei
  • 9,188
  • 10
  • 67
  • 113
Chris
  • 935
  • 3
  • 10
  • 21
  • 1
    Thanks, that's what I've finally done, but in my understanding that was a workaround. I thought that the normal behavior was loading all the "user" modules. – JoanComasFdz May 28 '14 at 12:19
3

I noticed that following structure is not supported by PowerShell 4:

Modules\MySnapIn\1.0.0\MySnapIn.psm1

Works fine after update to version 5.

Der_Meister
  • 4,771
  • 2
  • 46
  • 53
1

Note: I'm authoring only script modules, so I may be wrong.

PowerShell module autoload depends on command discovery. I suspect that if you create manifest (New-ModuleManifest) and name commands that your binary module exposes, autoloading should kick-in and load module if someone will try to use one of these commands:

New-ModuleManifest -Path MySnappin.psd1 -RootModule MySnappin.dll -CmdletsToExport Get-Foo, Set-Bar
BartekB
  • 8,492
  • 32
  • 33