Batch Script IF-Then Statement for Reg Query - batch-file

So, I am right now creating a script for our VDI environment where it will create a registry entry on the HKEY_CURRENT_USER key, if the key does not exist. I have never done an if-then in batch, so I am curious how best to go about doing it. Right now my code is:
REG QUERY HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced\TaskbarAl
IF %errorlevel%==0 GOTO INSTALL
REG ADD "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" /v "TaskbarAl" /t REG_DWORD /d 0 /reg:64
Not sure how best to do it, since this is the first batch script I've written with an if clause. But, basically if TaskbarAl doesn't exist, then add it to that directory.
I wrote out the above script, but not sure where to go from here.

If, as per my comment, you want the key, value, and data, to be implemented regardless of whether it exists or not, then:
#%SystemRoot%\System32\reg.exe Add "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" /V "TaskbarAl" /T REG_DWORD /D 0 /F 1>NUL
If you do want to add the value and data, only if the value does not already exist, (keeping the end users data untouched), then:
#%SystemRoot%\System32\reg.exe Query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" /F "TaskbarAl" /V /E 2>NUL 1>&2 || %SystemRoot%\System32\reg.exe Add "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" /V "TaskbarAl" /T REG_DWORD /D 0 1>NUL
As you can see, no IF statement was required. I made use of a conditional statement using || instead. That condition means, if the previous command was unsuccessful, (returned an error).
If you still wanted to use an IF, you could do that too:
#Echo Off
%SystemRoot%\System32\reg.exe Query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" /F "TaskbarAl" /V /E 2>NUL 1>&2
If Not ErrorLevel 1 Exit /B
Rem Your commands to install go here.
%SystemRoot%\System32\reg.exe Add "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" /V "TaskbarAl" /T REG_DWORD /D 0 1>NUL

Related

How to Loop Registry Value in Batchfile

I need help. I want to set all ConfigFlags"=dword:0000000(0)value from ConfigFlags"=dword:000000(20).
I want to use cmd to do all of these but here's my problem.
In HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\PCI there are tons of HardwareIDS.
Like:
1.VEN_1000&DEV_0054&SUBSYS_197615AD&REV_01
2.VEN_10EC&DEV_5287&SUBSYS_096A1025&REV_01
....
inside each VEN_IDS there is 4&36677646&0&00E2 this value is not static it change depends on VEN_IDS
Like:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\PCI\VEN_10EC&DEV_5287&SUBSYS_096A1025&REV_01\4&36677646&0&00E2 after ConfigFlags.
I want to Set all ConfigFlags inspite of Different VEN_IDS and Class.`
To perform the task, you need to first of all determine the subkeys under: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\PCI. You can do that directly with the Query command of reg.exe.
If you perform that command within a For loop, you can return each registry key as a variable and pass each to the Do portion, which holds your command to Add the intended value data.
The following example should assist you:
#Echo Off
SetLocal EnableExtensions
Set "RootKey=HKEY_LOCAL_MACHINE"
Set "ParentKey=SYSTEM\CurrentControlSet\Enum\PCI"
Set "ValueName=ConfigFlags"
Set "DataType=REG_DWORD"
Set "DataValue=0x0"
Set "RegCommand=%SystemRoot%\System32\reg.exe"
Set "FindStrCommand=%SystemRoot%\System32\findstr.exe"
For /F "Delims=" %%G In ('
%RegCommand% Query "%RootKey%\%ParentKey%" /S /F "%ValueName%" /V
/T %DataType% 2^>NUL ^| %FindStrCommand% /B "HK"
') Do %RegCommand% Add "%%G" /V "%ValueName%" /T %DataType% /D "%DataValue%" /F
What it does is pass the full registry key names of each entry which currently holds a value named ConfigFlags to another reg.exe command, which /Force Adds the REG_DWORD data 0x00000000.
Please note that as this script will be modifying the content of the protected HKEY_LOCAL_MACHINE root key, you will need to run it elevated, (as administrator), or as a user with the required write permissions.
If you open a Command Prompt using the Run as administrator option, you could just perform the task as a single line command:
#For /F Delims^= %G In ('reg.exe Query HKLM\SYSTEM\CurrentControlSet\Enum\PCI /S /F ConfigFlags /V /T REG_DWORD 2^>NUL^|findstr.exe "^HK"')Do #reg.exe Add "%G" /V ConfigFlags /T REG_DWORD /D 0 /F 1>NUL
If you don't want to just concentrate on PCI devices, to include for example: ACPI, DISPLAY, HID, ROOT, SCSI, USB etc., then change Enum\PCI to just Enum.

Batch File: If registry key's data equals x

I'm trying to check whether a recording device is enabled or disabled. I've tried a ton of examples I've found, but just can't get it to work. Both of the commands I use to change the data work.
Here's what I currently have:
REG QUERY "Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\MMDevices\Audio\Capture\{f2b4b04b-e32f-494a-94fc-f7846a7fc275}" /v
"DeviceState" | Find "1"
IF %ERRORLEVEL% == 1 goto turnoff
If %ERRORLEVEL% == 0 goto turnon
goto end
:turnon
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\MMDevices\Audio\Capture\{f2b4b04b-e32f-494a-94fc-f7846a7fc275}" /v DeviceState /d 1 /t Reg_DWord /f
:turnoff
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\MMDevices\Audio\Capture\{f2b4b04b-e32f-494a-94fc-f7846a7fc275}" /v DeviceState /d 0x10000001 /t Reg_DWord /f
goto end
:end
#exit
I'm not sure if the Find "1" should actually be 1. When I query that data and the device is enabled, the output is DeviceState REG_DWORD 0x1
and while the device is disabled, the output is DeviceState REG_DWORD 0x10000001
set key=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion^
\MMDevices\Audio\Capture\{f2b4b04b-e32f-494a-94fc-f7846a7fc275}
reg query "%key%" /v DeviceState | find "0x10000001"
if errorlevel 1 goto turnoff
goto turnon
:turnon
reg add "%key%" /v DeviceState /d 0x1 /t REG_DWORD /f
goto :eof
:turnoff
reg add "%key%" /v DeviceState /d 0x10000001 /t REG_DWORD /f
goto :eof
Or shorter code without using goto:
set key=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion^
\MMDevices\Audio\Capture\{f2b4b04b-e32f-494a-94fc-f7846a7fc275}
reg query "%key%" /v DeviceState | find "0x10000001"
if errorlevel 1 (set "data=0x10000001") else set "data=0x1"
reg add "%key%" /v DeviceState /d %data% /t REG_DWORD /f
The use of ^ at end of line is a line continuation character i.e.
the interpreter removes the ^ and joins the next line to the current line.
HKEY_LOCAL_MACHINE can be substituted with HKLM in the reg commands.
goto :eof a.k.a goto end of file will end the script.
Issues in your code:
Commands after :turnon label continue on to the :turnoff label
commands thus :turnon commands are undone.
find "1". 1 can be found in 0x1 and in 0x10000001. Advise
find "0x10000001" as it is unique to both data values.
Note:
I tested with HKLM\SOFTWARE\~Microsoft\... key instead of
HKLM\SOFTWARE\Microsoft\... key as on Win7, the key of
MMDevices and subkeys has SYSTEM permissions set and admin
and user groups do not have permission to write.

batch file not working for registry edit

I am trying to disable popup blocker using above script. This is not working and registry value is still 1 only after execution.
REG ADD "HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\New Windows" /v "PopupMgr" /t REG_DWORD /d 0 /f
There should be no reason why that data cannot be changed, are you sure you are altering and checking the key for the currently active user?I have tested the below script sucessfully, it toggles the pop-up blocker on or off depending upon it's current state.
#ECHO OFF
SET "KEY=HKCU\Software\Microsoft\Internet Explorer\New Windows"
FOR /F "SKIP=2TOKENS=3" %%A In ('REG QUERY "%KEY%" /V PopupMgr') DO SET "_=%%A"
IF %_% EQU 0 (SET/A _+=1) ELSE SET "_=0"
REG ADD "%KEY%" /V PopupMgr /T REG_DWORD /D %_% /F>NUL
Look at this article Windows Central it may help you with what you want.
If you cannot edit it, why don't you just delete the key and re-add it by using REG Delete

Batch File to Add Multiple Values if Condition True

I want to query registry for a value, if the value exists I want to modify it, if it doesn't exist I want to go to the next similar key and check.
Key:
HKCU\SOFTWARE\MySoftware\001
HKCU\SOFTWARE\MySoftware\002
HKCU\SOFTWARE\MySoftware\003
The value is the same for each key but I need to cycle through each one to check until I have checked all of them.
What I am currently doing is:
reg query "HKCU\SOFTWARE\MySoftware\001" /v MyValue
if %ERRORLEVEL%==0 (
reg add "HKCU\SOFTWARE\MySoftware\001" /v MyValue /f /t REG_DWORD /d 10
) else (
goto :KEY2
)
This will cycle through all the keys I need to check and modify each but it is quit a few lines and I am thinking perhaps I could build a subroutine of some type to accomplish this but I am kinda stumped.
The following code queries the registry key HKEY_CURRENT_USER\SOFTWARE\MySoftware for all immediate sub-keys, filters for those whose names consist of three decimal figures, loops through them in ascending order, queries the value MyValue for each and if found, modifies the respective data:
for /F delims^=^ eol^= %%K in ('
reg query "HKEY_CURRENT_USER\SOFTWARE\MySoftware" /K /F * ^
^| findstr "^HKEY_.*\\[0-9][0-9][0-9]$" ^| sort
') do (
reg query "%%K" /V "MyValue" && reg add "%%K" /V "MyValue" /f /t REG_DWORD /d 10
)
The used && operator lets the following command execute only if the preceding one succeeded.
for %%a in (001 002 003) do (
reg query "HKCU\SOFTWARE\MySoftware\%%a" /v MyValue
if not errorlevel 1 (
reg add "HKCU\SOFTWARE\MySoftware\%%a" /v MyValue /f /t REG_DWORD /d 10
)
)
should accomplish this. errorlevel is interpreted in its conventional manner on its run-time value. All you need do is modify the list in parentheses, each value of which is assigned in turn to the metavariable %%a

Read and write REG_DWORD from batch file

My requirement is to read a REG_DWORD from the registry and write it to another location. I have succeeded in reading from a registry location, but don't know how to write.
My code:
#echo off
REG QUERY HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\NetCache /v "OfflineDirRenameDelete"
SET previous=OfflineDirRenameDelete
REG ADD HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\MySoftware /v "CacheDelete" /t REG_DWORD /d previous /f
This code fails, as "reg add" doesn't understand the "previous" variable
I am able to hard-code a value and set it to the registry. For example, this works:
REG ADD HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\MySoftware /v "CacheDelete" /t REG_DWORD /d 5454 /f
But I basically don't know how to dynamic REG_DWORD value to the registry.
Use %previous% to get the content of the variable.
REG ADD HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\MySoftware /v "CacheDelete" /t REG_DWORD /d %previous% /f
Here is a quick reference on using variables in batch files.
The problem was that the REG QUERY instruction was returning something the name of the registry key, the type of it, and the value together. For example, it would be something like:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\CacheDelete
LogLevel REG_DWORD 0x29
And I was trying to set this value to another REG_DWORD in the registry, which obviously failed since this is more than a DWORD. And I may have had some syntax error and stuff in trying that too, so there's no wonder it failed.
The following code worked for me:
#ECHO OFF
set KEY_NAME="HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\NetCache"
set VALUE_NAME="OfflineDirRenameDelete"
FOR /F "usebackq skip=2 tokens=1,2*" %%A IN (
`REG QUERY %KEY_NAME% /v %VALUE_NAME% 2^>nul`) DO (
set "ValueName=%%A"
set "ValueType=%%B"
set Val="%%C"
)
reg add %KEY_NAME% /v "CacheDelete" /t REG_DWORD /d %Val% /f
Hope this helps someone!

Resources