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
$t$envarname = "PSModulePath"
$regkey = Get-Item -Path "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
$t$envar = $t$regkey.GetValue("PSModulePath"$envarname, "", "DoNotExpandEnvironmentNames")
ECHO $t$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
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.
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.