Set PSModulePath Environment Variable with PowerShell in Windows 10
PowerShell - Get OS Environmental Variables without Expanding
You can use the Get-Item cmdlet with the -path
parameter and then pass that the path of the registry key containing the PSModulePath
environmental variable.
You can then use the RegistryKey.GetValue Method along with DoNotExpandEnvironmentNames to get the string value of the PSModulePath
environmental variable without expanding it.
PowerShell
$envarname = "PSModulePath"
$regkey = Get-Item -Path "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
$envar = $regkey.GetValue($envarname, "", "DoNotExpandEnvironmentNames")
ECHO $envar
Note: You will want to be sure you run this from administrator elevated PowerShell command prompt or ISE screen for it to work correctly.
Further Resources
-
HowTo: Set an Environment Variable in Windows - Command Line and Registry
The location of the user variables in the registry is:
HKEY_CURRENT_USER\Environment
. The location of the system variables in the registry is:HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
When setting environment variables through the registry, they will not recognized immediately. One option is to log out and back in again. However, we can avoid logging out if we send a WM_SETTINGCHANGE message, which is just another line when doing this programatically, however if doing this on the command line it is not as straightforward.
-
Retrieves the value associated with the specified name and retrieval options. If the name is not found, returns the default value that you provide.
-
RegistryKey.GetValue Method (String, Object, RegistryValueOptions)
Use this overload to specify special processing of the retrieved value. For example, you can specify RegistryValueOptions.DoNotExpandEnvironmentNames when retrieving a registry value of type RegistryValueKind.ExpandString to retrieve the string without expanding embedded environment variables.
Related videos on Youtube
jippyjoe4
Updated on September 18, 2022Comments
-
jippyjoe4 over 1 year
I don't understand this. So currently my system environment variable named "PSModulePath" looks like this:
%ProgramFiles%\WindowsPowerShell\Modules;%SystemRoot%\system32\WindowsPowerShell\v1.0\Modules
Now observe the following PowerShell script:
$envarname = "PSModulePath" $envar = (get-item env:$envarname).Value [Environment]::SetEnvironmentVariable($envarname, $envar + ";C:\Expedited", "Machine")
All it should be doing is adding the path "C:\Expedited" to the PSModulesPath environment variable, right? Well, after running this script as administrator, the PSModulePath environment variable changes into this:
C:\Users\Username\Documents\WindowsPowerShell\Modules;C:\Program Files\WindowsPowerShell\Modules;C:\Windows\system32\WindowsPowerShell\v1.0\Modules;C:\Expedited
Notice how:
- There were originally two paths, each of which contained percentage signs (variables) in the original, but afterward they all changed directly into hard-coded paths.
- The "C:\Users\Username\Documents\WindowsPowerShell\Modules" path sprung out of nowhere (it wasn't in the original!)
I don't have any idea why either of these two things happened. When adding a path to this variable, I would like to keep it as close to the original as possible, not make all these other changes. Is there any way to preserve the percentage signs that were lost? How do I edit this environment variable correctly from within PowerShell?
-
Ramhound almost 6 years"C:\Users\Username\Documents\WindowsPowerShell\Modules" - This would only exist in the user's PATH variable. It appears you are modifying the user's PATH variable instead of the system's PATH variable hence the difference. This question has an answer that explains the differences.
-
jippyjoe4 almost 6 years@PimpJuiceIT, just open System Properties\Advanced\Environment Variables and take a look in there yourself. If you use the actual UI, or even check the corresponding registry entry, you can see the percentage signs in the text string. The problem is that I cannot get the string with the percentage signs using PowerShell for some reason; they always get converted to hard-coded paths and I don't know why.
-
jippyjoe4 almost 6 yearsYou're right, that method is simpler than my code, but it still results in the same two undesired effects I listed in my original question. Do you have an explanation for those?
-
Vomit IT - Chunky Mess Style almost 6 yearsjippyjoe4 - This explains why and gives a way to get the OS Environmental Variables without expanding other environmental variables within their values to retain the percent sign. In case it's not clear as to why explicitly though, it's because the other variables within that variable are being expanded so this is a way to prevent with simple to follow code that's not very complex.