Powershell: Environmental variable not reflected

Created on 11 Jan 2018  路  6Comments  路  Source: PowerShell/PowerShell

I can鈥檛 seem to think of valid use case for the reported steps (running Windows PowerShell from PowerShell Core console). However, it鈥檚 something unexpected so bringing it to your attention.

Steps to reproduce

Environment: PowerShell Core 6 (GA) on Windows

In the pwsh:

  1. run powershell
  2. $env:psmodulepath

Expected behavior

The psmodulepath should be the value from Windows PowerShell.

Actual behavior

The value is from the pwsh session.

Issue-Question Resolution-Duplicate

All 6 comments

I can not repo.
If you first run Windows PowerShell do you see $env:psmodulepath?

I can repro this. PS Core has a separate PSModulePath environment variable because of the separation from Windows PowerShell modules. unfortunately, when you start Windows PowerShell from PowerShell Core, Windows PowerShell sees the PSModulePath environment path and adopts it. If you do this the other way around and start PowerShell Core from Windows PowerShell, PowerShell core ignores the PSModulePath environment variable and uses its own.

In PowerShell Core:

$env:PSModulePath; powershell -c '$env:PSModulePath'
C:\Users\username\Documents\PowerShell\Modules;C:\Program Files\PowerShell\Modules;c:\program files\powershell\6.0.0-rc.2\Modules
C:\Users\username\Documents\PowerShell\Modules;C:\Program Files\PowerShell\Modules;c:\program files\powershell\6.0.0-rc.2\Modules

In Windows PowerShell:

$env:PSModulePath; pwsh -c '$env:PSModulePath'
C:\Users\username\Documents\WindowsPowerShell\Modules;C:\Program Files\WindowsPowerShell\Modules;C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\;C:\Program Files\Intel\;C:\opscode\chefdk\modules\
C:\Users\username\Documents\PowerShell\Modules;C:\Program Files\PowerShell\Modules;c:\program files\powershell\6.0.0-rc.2\Modules

Yes, I semms mixed the directions.

It is dup #2636

Thank you guys. I checked the discussion in #2636. I see the behavior is caused from the subprocess inherits the variable from its parent now. Wondering why it's does not behave the same the other way around though (launching PowerShell Core from Windows PowerShell)? Launching Windows PowerShell from PowerShell Core may be a less common use case. However, it definitely cause inconsistent user experience.

@LawrenceHwang Because PowerShell Core needs to ignore the Windows PowerShell PSModulePath. PSModulePath is set on your system and passed to Windows PowerShell when it starts you can see this by starting cmd.exe form start and running echo %PSModulePath%. PowerShell Core needs to ignore this by default to prevent Windows Powershell Modules from loading by default and to find the PowerShell Core modules.

This is ridiculous.

If you're not actually reading the variable from the environment that's not really an environment variable, is it?

Was this page helpful?
0 / 5 - 0 ratings