Powershell: PSModulePath doesn't use $home, but uses [environment]::getfolderpath("mydocuments")

Created on 4 Mar 2020  路  9Comments  路  Source: PowerShell/PowerShell

It appears that env:PSModulePath is being prepended with [environment]::getfolderpath("mydocuments")\WindowsPowershell\Modules rather than $Home\Documents\WindowsPowershell\Modules as described in the docs

Steps to reproduce

Redirect your mydocuments to a different folder

Start powershell and get the $env:psmodulepath

Documented behavior

$env:PSModulePath.split(";") -contains "$home\WindowsPowershell\Modules"

Actual behavior

 $env:PSModulePath.split(";") -contains "$([environment]::getfolderpath("mydocuments"))\WindowsPowershell\Modules"
Issue-Question

All 9 comments

What PowerShell version are you seeing this in?

All versions

Simon Sabin

All versions

The path you give in your example seems to be a Windows PowerShell path. This repository tracks issues in PowerShell 6+.

The module path is set here:

https://github.com/PowerShell/PowerShell/blob/20a236b228d024ed23bf12c9331bbbb484e3836c/src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs#L968-L972

It seems that $HOME variable comes from the $env:USERPROFILE environment variable:

https://github.com/PowerShell/PowerShell/blob/a34d0f3e808659b0c07b8fdac83aaae1dae43c21/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs#L182

It's not clear whether the inconsistency is intended, but given that this behaviour is longstanding and likely unable to be changed, we might want to move this issue to the docs repo.

I think it is Docs issue.

Its a fairly broad reaching docs problem. I'll raise a docs issue.

@joeyaiello or @sdwheeler can you transfer this issue to the docs repo?

I鈥檝e already created an issue on the docs side. You can close this one

Simon Sabin

As an aside, regarding ships that have sailed: The more appropriate definition of $HOME would be "${env:HOMEDRIVE}${env:HOMEPATH}" rather $env:USERPROFILE - while the two values are _by default_ identical, they can differ, and the former is then the better location for modules.

Therefore, basing the personal module path on [environment]::getfolderpath("mydocuments") is actually more appropriate than basing it on $HOME.

I agree that its appropriate use, its just confusing that its not documented.
Furthermore the rules for how its set if the user environment variable is set isn't clear in the docs

Was this page helpful?
0 / 5 - 0 ratings