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?
I noticed that following structure is not supported by PowerShell 4:
Works fine after update to version 5.
A checklist that may help you identify the issue:
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?
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 justC:\PowerShell\Modules
. What is the value of your$env:PsModulePath
?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.
The
$PSModuleAutoLoadingPreference
preference variable can be used to turn off autoloading; however, unless you have explicitly changed it, it defaults toAll
so likely that is not the problem (about_Preference_Variables shows you the possible values). What is your value of$PSModuleAutoLoadingPreference
?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 viaImport-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 fromCmdlet
orPSCmdlet
, as detailed in Writing a Windows PowerShell Cmdlet).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: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:If the
Microsoft.PowerShell_profile.ps1
file does not exist just create it.