Home > OS >  CMD check if registry key exist
CMD check if registry key exist

Time:09-29

I have multiple registry keys called

  • O365BusinessRetail - en-us
  • O365BusinessRetail - de-de
  • ... and so on for many languages

I want to check if the registry keys exist or not. But this command will not work "Registry key could not be found"

reg query "HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall\O365BusinessRetail*"

IF %ERRORLEVEL% == 1 exit 1
If %ERRORLEVEL% == 0 exit 0

I also tried

reg query "HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall" /v ? | findstr /C "O365BusinessRetail*"

IF %ERRORLEVEL% == 1 exit 1
If %ERRORLEVEL% == 0 exit 0

However with Powershell it works but I can't use it because Powershell is blocked on our side

if (Test-Path -Path registry::HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall\O365BusinessRetail*) {
  exit 1  # same as: Write-Output 1
}
else {
  exit 0  # same as: Write-Output 0
}

What is the best way to do that in CMD?

Thanks

CodePudding user response:

Your submitted code should look like this:

@SystemRoot%\System32\reg.exe Query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" /F "O365BusinessRetail*" /K 1>NUL 2>&1
@Exit %Errorlevel%

If one or more subkeys beginning with the string O365BusinessRetail exist, the script will exit with the 0 error code. If none exist, it will exit with a 1 error code.

Please also note that it is possible that the applications are registered under another uninstall key branch or root key.

  • Related