BLOG
Wissenstransfer von IT-Spezialisten
| |

Import-Module AzureRM or PackageManagement fails as User #PowerShell, #PowerShellGet, #PackageManagement

The use of Azure Key Vault requires the use of PowerShell (today). Prerequisite is the PowerShell module AzureRM. This can be installed by PowerShell Package Manager using the PowerShell PowerShell Gallery (see https://azure.microsoft.com/en-us/documentation/articles/powershell-install-configure). With my local admin account it’s possible to import the PowerShell PackageManagement but not with my normal user account.

As user:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
PS H:\> Import-Module PackageManagement
import-module : The specified module 'PackageManagement' was not loaded because no valid module file was found in any module directory.
At line:1 char:1
+ import-module PackageManagement
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (PackageManagement:String) [Import-Module], FileNotFoundException
    + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
PS H:\>

As admin:

1
2
3
PS H:\> Import-Module PackageManagement
PS H:\>

Reason:

In my users session the path to the PowerShell modules in „C:\Program Files\WindowsPowerShell\Modules“ is missing.

1
2
3
PS H:\> $env:PSModulePath
C:\Windows\system32\WindowsPowerShell\v1.0\Modules\;C:\Program Files (x86)\Microsoft SQL Server\120\Tools\PowerShell\Modules\;C:\Program Files\Microsoft System Center 2012 R2\Operations Manager\Powershell\;C:\Program Files\Microsoft Azure Recovery Services Agent\bin\Modules\;C:\Program Files (x86)\Microsoft SDKs\Azure\PowerShell\ServiceManagement

In my case the use-specific variable PSModulePath overrides the system-wide variable for PSModulePath (which contains the path to „C:\Program Files\WindowsPowerShell\Modules“). My admin account has no user-specific PSModulePath variable and therefore the modules can load.

Solution:

Add the path for the current PowerShell session in the context of the current user with:

1
$env:PSModulePath = $env:PSModulePath + "; C:\Program Files\WindowsPowerShell\Modules"

Or:

Remove the custom variable for PSModulePath (caution: intended customizations are lost):

1
[Environment]::SetEnvironmentVariable("PSModulePath", "", "User")

Restart the PowerShell Session