Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell v4 not importing module automatically

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?

like image 601
JoanComasFdz Avatar asked Nov 28 '22 20:11

JoanComasFdz


2 Answers

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).

like image 92
Michael Sorens Avatar answered Nov 30 '22 10:11

Michael Sorens


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.

like image 42
Chris Avatar answered Nov 30 '22 11:11

Chris