Home > Enterprise >  Batch file REG QUERY
Batch file REG QUERY

Time:12-17

I am writing a script a retrieve the value of the "DefaultUserName" registry key. I have:

@echo off
Title Kiosk Account Autologin Password Changer
::Search for current Kiosk Account . . .
echo Identifying Kiosk Account
reg query HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon /s
Pause

but I keep getting a syntax error. I am a beginner at coding and all help is appreciated.

The end goal is to have an input output table to automate the change of the "DefaultPassword" key depending on what the user name is. The company I work for is changing all of the passwords and I don't want to have to do it all by hand.

CodePudding user response:

Given your additional requirements as stated in you comment, this should add the required password string value data, only if the existing default user name was exactly the case insensitive string A.

Please note, because you are making modifications within the protected HKEY_LOCAL_MACHINE root key, the examples below will need to be run with elevated privileges.

@%SystemRoot%\System32\reg.exe Query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /V "DefaultUserName" 2>NUL | %SystemRoot%\System32\findstr.exe /IRC:"^  *DefaultUserName  *REG_SZ  *A$" 1>NUL && %SystemRoot%\System32\reg.exe Add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /V "DefaultPassword" /T "REG_SZ" /D "1" /F 1>NUL

The only thing which you may not understand here is the conditional operator &&. There are two conditional operators, && and ||, the former means if the previous command reported as successful, and the latter means if the previous command reported as unsuccessful. So what this does in the above case, is findstr.exe looks for a line which begins with one or more space characters, followed by the case insensitive string DefaultUserName, followed by one or more spaces, followed by the case insensitive string REG_SZ followed by one or more spaces, and ending with the case insensitive string A. To learn more about the FindStr command utility options, please open a Command Prompt window, type findstr /? and press the ENTER key. If the returned string matches, it is printed via StdOut to the console window, so I have redirected that printing to the NUL device, because we do not need to see or use it, and the command following the && is run. (If it doesn't match the rest of the line will not be run).

For modifying the command line, because let's be honest, you haven't really got a user name of A, just change out A in the FindStr match, as needed. So for example if your were really looking for a default user name of Michael you'd use:

@%SystemRoot%\System32\reg.exe Query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /V "DefaultUserName" 2>NUL | %SystemRoot%\System32\findstr.exe /IRC:"^  *DefaultUserName  *REG_SZ  *Michael$" 1>NUL && %SystemRoot%\System32\reg.exe Add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /V "DefaultPassword" /T "REG_SZ" /D "1" /F 1>NUL

Please however remember the output from your findstr /? command. You will note that some characters are special, so all user name strings may not work without escaping some special metacharacters.

CodePudding user response:

Just put quotes in the actual command line:

reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /s

Without them, the space at Windows NT causes the syntax error, since then it treats NT\CurrentVersion\Winlogon as a passed (third) argument to reg query command.

  • Related