Home > Blockchain >  How to use cmd to get full value of a registry key with with space?
How to use cmd to get full value of a registry key with with space?

Time:08-24

I want to use cmd to get PATH (enviroment variable) in registry with cmd. and here is the root of the value:HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Session Manager\Environment and the value of Path in the dictionary is the one i want to get value. i've tried this reg query "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Session Manager\Environment" /v "Path",but the output of it is this:

HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Session Manager\Environment
    Path    REG_EXPAND_SZ    D:\VMware Workstation Pro\bin\;C:\Program Files\Microsoft\jdk-11.0.12.7-hotspot\bin;%INTEL_DEV_REDIST%redist\intel64\compiler;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;%SYSTEMROOT%\System32\OpenSSH\;C:\Program Files\dotnet\;C:\Program Files (x86)\dotnet\;D:\ffmpeg\bin;D:\mingw64\bin;

I want to get D:\VMware Workstation Pro\bin\;C:\Program Files\Microsoft\jdk-11.0.12.7-hotspot\bin;%INTEL_DEV_REDIST%redist\intel64\compiler;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;%SYSTEMROOT%\System32\OpenSSH\;C:\Program Files\dotnet\;C:\Program Files (x86)\dotnet\;D:\ffmpeg\bin;D:\mingw64\bin in the output. i've tried a lot on the internet.But many of the answers are not support space(because there are some space in the value) or with bugs.

I want to know ho to fix the problem.

Thank you.

CodePudding user response:

Please do not use ControlSet001 ControlSet002 ControlSet003 etc. To be sure you are getting the content from the currently running Windows session, use CurrentControlSet instead. To output only the content you require you need to isolate it from the command results. You can do that by using a FOR /F loop, and selecting the appropriate options:

From

For /F "EOL=H Tokens=2,*" %G In ('%SystemRoot%\System32\reg.exe Query "HKLM\SYSTEM\CurrentControlset\Control\Session Manager\Environment" /V Path 2^>NUL') Do @Echo %H

From a

@For /F "EOL=H Tokens=2,*" %%G In ('%SystemRoot%\System32\reg.exe Query
 "HKLM\SYSTEM\CurrentControlset\Control\Session Manager\Environment" /V Path
 2^>NUL') Do @Echo %%H

As you can see the content is assigned to %H in or %%H in a , and output in this case, using the ECHO command

  • Related