Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does PSModulePath environment property get composed?

Tags:

powershell

This question is related to my other post - Using both desktop Powershell 5.1 and Powershell Core 6.1

Basically the root of the evil is that when I open a desktop Powershell and check $env:PSModulePath I see PS.Core module path there.

Please, observe:

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

PS C:\Users\me> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.17763.503
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.17763.503
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1


PS C:\Users\me> $env:PSModulePath -split ';'
C:\Users\me\Documents\PowerShell\Modules
C:\Program Files\PowerShell\Modules
c:\program files\powershell\6\Modules
C:\Program Files\WindowsPowerShell\Modules
C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules
PS C:\Users\me>

Notice c:\program files\powershell\6\Modules.

Now when I inspect the Environment variables in the Control Panel I see a different picture:

User

enter image description here

System

enter image description here

I do not even have the $PROFILE script:

PS C:\Users\me> Test-Path $PROFILE
False
PS C:\Users\me>

As one can see PS.Core 6.1 module path is not mentioned in the Control Panel, yet it is found in the actual PSModulePath environment variable.

So, my question is - how is PSModulePath truly composed?

I could not find it in the Microsoft documentation (I mean I did find it is an environment variable, but as one can see there is more to it). I must be missing something obvious.

EDIT 1

I opened Process Explorer and checked the environment of the parent process - explorer.exe. Its PSModulePath is:

C:\Users\mkharitonov\Documents\PowerShell\Modules
C:\Program Files\PowerShell\Modules
c:\program files\powershell\6\Modules
C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules

But after killing the explorer.exe and running it again, I can see the PS.Core module path disappeared. And so reopening the Powershell shows proper module path.

I suppose when I installed PS.Core 6.1 it modified the environment of the explorer.exe somehow. Or was it VS Code? Anyway, restarting the explorer fixes the issue.

I will have to dig into it and open a bug either to PS.Core or VS Code.

like image 877
mark Avatar asked Jul 27 '26 00:07

mark


1 Answers

When PowerShell starts up it'll attempt to set the PSModulePath env var by:

  1. Reading the existing value of %PSModulePath% from the host process
  2. Reading default module paths for the current user and for all users from the host application's config
  3. If on Windows, reading the %windir%\system32 module folder as well
  4. Update the value of $env:PSModulePath by combining all three

If you see an unexpected value in there, pwsh.exe might have simply inherited it frmo the parent process and it's included in step 1

like image 111
Mathias R. Jessen Avatar answered Jul 28 '26 13:07

Mathias R. Jessen