The link between PSModulePath environment variable and Import-Module Powershell cmdlet

Recently while working with one of the Windows 10 Pro machines, encountered this error while launching PowerShell command line automatically through Sitecore Complete Install Assistant (SCIA)


Error:

Import-Module : The module 'Microsoft.PowerShell.Utility' could not be loaded. For more information, run

'Import-Module Microsoft.PowerShell.Utility'.

+ Import-Module PowerShellGet

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : ObjectNotFound: (Microsoft.Power...t-LocalizedData:String) [Import-Module], CommandNotFo

   undException

    + FullyQualifiedErrorId : CouldNotAutoLoadModule,Microsoft.PowerShell.Commands.ImportModuleCommand

Note that if the PowerShell command line is launched manually by typing PowerShell through start menu, on picking the first PS option from the list, and then executing the script, the command line didn't throw the above error. 


On the otger hand, there was a different error this time that I wasn't worried much at the moment:

The line that caused the original error was this - 

More specifically, this line is part of an auto-generated PS script file that populates env variables for Docker.

Anyway, the reason for the error is, the runtime was picking the wrong PowerShell exe and missing modules for execution. So, I decided to investigate and fix the root cause in the concerned machine.

As I started investigating the system and environment variables, I noticed that the PSModulePath variable was pointing to C:\Users\Navan Sundarrajan\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Windows PowerShell

I decided to compare with a machine that worked fine and found that the concerned variable was pointing to %SystemRoot%\system32\windowspowershell\v1.0\Modules. 


Also, in the concerned path, I could find the Microsoft.PowerShell.Utility folder -


Beware to put the correct spelling for the folder path if you are typing in the path. I had missed  one of the "s" in windowspowershell and so was still stuck with the same error since the path was non-existent!

When I put the correct path for this variable and restarted the machine, the  PowerShell script execution was successful. 

In a gist, whenever you use Import-Module, ensure that the PSModulePath environment variable lists WindowsPowershell modules path.



Comments