[Bat] How to get the registry value in a batch file! Introducing how to use "reg" and how to retrieve only the value!

Windows

This time, I will introduce how to get the registry.

Method using Reg command

First, try using the Reg command.This time, let's take the release build number of Windows 10 as an example.

The release build number for Windows 10 is stored below.

Key:HKLM: \ SOFTWARE \ Microsoft \ Windows NT \ CurrentVersion
Subkey:ReleaseID

reg query "HKLM \ SOFTWARE \ Microsoft \ Windows NT \ CurrentVersion"/ v ReleaseID

This is the basic usage of the Reg Query command.
This time, the registry key name contains a half-width space, so it is enclosed in "" ".

In this site, when you copy "" ", it may be full-width.In that case, please replace it with a half-width "" "after pasting.

The problem with this method is that the Reg Query output spans two lines and even displays extra information.

To solve this, try the following method.

How to use PowerShell

Now let's try it with PowerShell.

powershell (Get-ItemProperty'HKLM: \ SOFTWARE \ Microsoft \ Windows NT \ CurrentVersion').ReleaseID

In PowerShell, you can get the registry with the Get-ItemProperty command. PowerShell uses "'" instead of "" ".

Get the values ​​of the subkeys included in the key at once, and select the subkey for which you want the value.. [Subkey name]If you specify it in the form of, you can easily get only the value.

With these two methods, passing values ​​within a batch is very cumbersome.
(Cannot be stored in a variable with the set command etc.)

The same effect can be obtained by the method shown below, and since it is stored in a variable, it is very easy to handle in a batch.

How to get the result of Reg command with For command

This is a commonly used type of method.
The return value of the Reg command is retrieved and stored only where it is needed by the For statement.

for / f "tokens = 3" %% A in ('reg query "HKLM \ SOFTWARE \ Microsoft \ Windows NT \ CurrentVersion " / v "Release ID "') do set REG_RESULT = %% A

echo% REG_RESULT%

"%%" in the For statement cannot be executed normally unless one "%" is deleted when executing it directly on the command line.
Example) %% A →% A

In For / f, the return value of the command enclosed in "'" in () is read, and by default, it is read into the variables% A,% B,% C ... with a half-width space as the delimiter.

This time, the Tokens option is specified, and the third read value from the return value of the Reg Query command is stored in% A.

after thatPull out to REG_RESULT variableechoing.That part becomes the registry value itself.

If there is a space in the registry key name, the number in Tokens must be increased by that number.

To solve this problem, you have to use a command that prints the value directly.The PowerShell command I mentioned earlier should solve that.Let's read it immediately.

Get the PowerShell result with the For command

I will try it for PowerShell as well as for the Reg command.

for / f "tokens = * usebackq" %% A in (%% A in (`"@powershell (Get-ItemProperty'HKLM: \ SOFTWARE \ Microsoft \ Windows NT \ CurrentVersion'). ReleaseID"`) do set REG_RESULT = %% A

"%%" in the For statement cannot be executed normally unless one "%" is deleted when executing it directly on the command line.
Example) %% A →% A

In PowerShell, the value is output as it is when the command is executed, so specify "*" (all) for Tokens.

In addition, since duplication occurs between the "'" specification in For and the "'" specification in PowerShell, the usebackq option is added to replace the "'" on the For statement side with "` ".

Since this PowerShell command has a lot of blanks, enclose it with "" "to consider it as one command.

This way you can be sure to get the registry value.

Summary

Getting the registry is often used, but it's often difficult to get it into a batch.

Personally, I often get it in the form of a combination of For and PowerShell.After all, I don't want to be influenced by the environment and key names.

I hope it helps you.

Comment

Translate »
I copied the title and URL