PowerShell - Obtenha variáveis ambientais do SO sem expandir
Você pode usar o cmdlet Get-Item com o parâmetro -path
e passar o caminho do a chave do registro que contém a variável ambiental PSModulePath
.
Você pode então usar o RegistryKey.GetValue Método juntamente com DoNotExpandEnvironmentNames para obtenha o valor da string da variável ambiental PSModulePath
sem expandi-la.
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.
Maisrecursos
HowTo: defina um Variável de Ambiente no Windows - Linha de Comando e Registro 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.
-
Método RegistryKey.GetValue (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.