Error when running Import-Module
I am quite sure, everyone who already installed PowerShell modules knows this error running Import-Module:
Import-Module : File C:\Program Files\WindowsPowerShell\Modules\VMware.VimAutomation.Sdk\22.214.171.12464823\VMware.VimAutomation.Sdk.psm1 cannot be loaded because its operation is blocked by software
restriction policies, such as those created by using Group Policy.
At line:1 char:1
+ import-module vmware.powercli
+ CategoryInfo : SecurityError: (:) [Import-Module], PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess,Microsoft.PowerShell.Commands.ImportModuleCommand
Normally this is caused by – lets say – source restrictions of module. Here are three possible causes of this error and how to fix it.
PowerShell Execution Policy
PowerShell can be configured to prevent running scripts (like modules) or single commands when they could harm your system. Here is a good explanation of this topic. Current settings for all scopes can be shown by running:
To set another policy, use:
But group policies can overwrite your settings! Therefore you do not always have the option to change this policy.
When you try to import a non-signed module that was downloaded from the internet you get the error when your execution policy is set to RemoteSigned. This is because downloaded files from the internet are marked as such. To “un-mark” these files, you can run:
Get-ChildItem -Path Path_to_module -Recurse -File | Unblock-File
Recently I could not solve the error with solutions above. I did some troubleshooting and found out, certificate of PowerCLI module were not trusted on my system.
What did not solve the problem:
- Install certificate.
- Reinstall module.
- Save module (
Save-Module) on a working system and import it.
I found out, certificate made its way into Untrusted Certificates. I do not know how this did happen, but it did.
After moving certificate to a trusted folder – like Trusted Publishers – importing module worked again without any issues.