Skip to main content
Discovered it was a bit more complicated
Source Link

Environment variables expansion has been tackled in other answers, but as to why "C:\Users\Username\Documents\WindowsPowerShell\Modules" appears out of nowhere:

PSModulePath is a tricky beast. It does not act as others environment variables.

For mostOutside of Powershell, like any other environment variablesvariable, if it exists both in the user's and the machine's environment, the machine's one is discarded.

But inside of Powershell, PSModulePath on the other end is a concatenation: the user one + the computer one. (A bit like "Path", but funnily enough, "Path" works the other way round: computer THEN user).

That's for outside of Powershell (like, if you look at it from inside CMD). When you enter Powershell, it gets a bit more complicated:

The default machine PSModulePath is "%ProgramFiles%\WindowsPowerShell\Modules;%SystemRoot%\system32\windowspowershell\v1.0\Modules". If you define a machine PSModulePath that does not contain those two paths, Powershell automatically adds them.

(There are two paths because the modules in %SystemRoot% are compatible with both 32 and 64 bits version of Powershell, whereas those in %ProgramFiles% are not (the trick being that %ProgramFiles% resolves to "Program Files (x86)" for the 32 bits version).)

There is a default PSModulePath for the user too, and it's %USERDOC%\WindowsPowerShell\Modules. But it would be too simple if it acted as its machine counterpart: if the user PSModulePath is not defined, then from within Powershell, its value is set to %USERDOC%\WindowsPowerShell\Modules, that's your 'it comes out of nowhere'.

Now if PSModulePath is defined for the user, its value is just what is defined, and %USERDOC%\WindowsPowerShell\Modules is not added.

I don't know if I'm clear. But I've been bitten by a PSModule problem recently, and I can't seem to find that technical information anywhere.

(Final note: I won't give the details, but all I wrotehere is valid fo Windos Powershell only (v5). Powershell7 adds new paths.)

Environment variables expansion has been tackled in other answers, but as to why "C:\Users\Username\Documents\WindowsPowerShell\Modules" appears out of nowhere:

PSModulePath is a tricky beast. It does not act as others environment variables.

For most environment variables, if it exists both in the user's and the machine's environment, the machine's one is discarded.

PSModulePath on the other end is a concatenation: the user one + the computer one. (A bit like "Path", but funnily enough, "Path" works the other way round: computer THEN user).

That's for outside of Powershell (like, if you look at it from inside CMD). When you enter Powershell, it gets a bit more complicated:

The default machine PSModulePath is "%ProgramFiles%\WindowsPowerShell\Modules;%SystemRoot%\system32\windowspowershell\v1.0\Modules". If you define a machine PSModulePath that does not contain those two paths, Powershell automatically adds them.

(There are two paths because the modules in %SystemRoot% are compatible with both 32 and 64 bits version of Powershell, whereas those in %ProgramFiles% are not (the trick being that %ProgramFiles% resolves to "Program Files (x86)" for the 32 bits version).)

There is a default PSModulePath for the user too, and it's %USERDOC%\WindowsPowerShell\Modules. But it would be too simple if it acted as its machine counterpart: if the user PSModulePath is not defined, then from within Powershell, its value is set to %USERDOC%\WindowsPowerShell\Modules, that's your 'it comes out of nowhere'.

Now if PSModulePath is defined for the user, its value is just what is defined, and %USERDOC%\WindowsPowerShell\Modules is not added.

I don't know if I'm clear. But I've been bitten by a PSModule problem recently, and I can't seem to find that technical information anywhere.

Environment variables expansion has been tackled in other answers, but as to why "C:\Users\Username\Documents\WindowsPowerShell\Modules" appears out of nowhere:

PSModulePath is a tricky beast. It does not act as others environment variables.

Outside of Powershell, like any other environment variable, if it exists both in the user's and the machine's environment, the machine's one is discarded.

But inside of Powershell, PSModulePath is a concatenation: the user one + the computer one. (A bit like "Path", but funnily enough, "Path" works the other way round: computer THEN user).

The default machine PSModulePath is "%ProgramFiles%\WindowsPowerShell\Modules;%SystemRoot%\system32\windowspowershell\v1.0\Modules". If you define a machine PSModulePath that does not contain those two paths, Powershell automatically adds them.

(There are two paths because the modules in %SystemRoot% are compatible with both 32 and 64 bits version of Powershell, whereas those in %ProgramFiles% are not (the trick being that %ProgramFiles% resolves to "Program Files (x86)" for the 32 bits version).)

There is a default PSModulePath for the user too, and it's %USERDOC%\WindowsPowerShell\Modules. But it would be too simple if it acted as its machine counterpart: if the user PSModulePath is not defined, then from within Powershell, its value is set to %USERDOC%\WindowsPowerShell\Modules, that's your 'it comes out of nowhere'.

Now if PSModulePath is defined for the user, its value is just what is defined, and %USERDOC%\WindowsPowerShell\Modules is not added.

I don't know if I'm clear. But I've been bitten by a PSModule problem recently, and I can't seem to find that technical information anywhere.

(Final note: I won't give the details, but all I wrotehere is valid fo Windos Powershell only (v5). Powershell7 adds new paths.)

Source Link

Environment variables expansion has been tackled in other answers, but as to why "C:\Users\Username\Documents\WindowsPowerShell\Modules" appears out of nowhere:

PSModulePath is a tricky beast. It does not act as others environment variables.

For most environment variables, if it exists both in the user's and the machine's environment, the machine's one is discarded.

PSModulePath on the other end is a concatenation: the user one + the computer one. (A bit like "Path", but funnily enough, "Path" works the other way round: computer THEN user).

That's for outside of Powershell (like, if you look at it from inside CMD). When you enter Powershell, it gets a bit more complicated:

The default machine PSModulePath is "%ProgramFiles%\WindowsPowerShell\Modules;%SystemRoot%\system32\windowspowershell\v1.0\Modules". If you define a machine PSModulePath that does not contain those two paths, Powershell automatically adds them.

(There are two paths because the modules in %SystemRoot% are compatible with both 32 and 64 bits version of Powershell, whereas those in %ProgramFiles% are not (the trick being that %ProgramFiles% resolves to "Program Files (x86)" for the 32 bits version).)

There is a default PSModulePath for the user too, and it's %USERDOC%\WindowsPowerShell\Modules. But it would be too simple if it acted as its machine counterpart: if the user PSModulePath is not defined, then from within Powershell, its value is set to %USERDOC%\WindowsPowerShell\Modules, that's your 'it comes out of nowhere'.

Now if PSModulePath is defined for the user, its value is just what is defined, and %USERDOC%\WindowsPowerShell\Modules is not added.

I don't know if I'm clear. But I've been bitten by a PSModule problem recently, and I can't seem to find that technical information anywhere.