windows 通过批处理语法查询Windows注册表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3749611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:16:51  来源:igfitidea点击:

Query the Windows registry through batch syntax

windowsbatch-fileregistry

提问by Luca Matteis

I'm trying to query a particular registry folder (or whatever you want to call it) to obtain some information.

我正在尝试查询特定的注册表文件夹(或任何您想调用的文件夹)以获取一些信息。

Particularly the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstallfolder contains a list of installed software.

特别是该HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall文件夹包含已安装软件的列表。

The issue is that each software is identified through a random key value like {0001B4FD-9EA3-4D90-A79E-FD14BA3AB01D}instead of the actual software (like Skype).

问题是每个软件都是通过一个随机的键值来标识的,{0001B4FD-9EA3-4D90-A79E-FD14BA3AB01D}而不是实际的软件(如Skype)。

This makes it hard to find the Skypeidentifier because I need to loop through everything inside this Uninstallfolder and check whether the DisplayNamevalue corresponds to Skype(or whatever other application name).

这使得很难找到Skype标识符,因为我需要遍历此Uninstall文件夹中的所有内容并检查该DisplayName值是否对应Skype(或任何其他应用程序名称)。

I need to use batch syntax... this is what I have so far but it doesn't behave the same on different computers, maybe I get different variables assigned based on some erroneous formatting of the reg output? I don't know. Can I use a data structure to contain whatever regoutputs? Anything would work.

我需要使用批处理语法......这是我到目前为止所拥有的,但它在不同的计算机上的表现并不相同,也许我根据 reg 输出的某些错误格式分配了不同的变量?我不知道。我可以使用数据结构来包含任何reg输出吗?什么都行。

@echo off
for /f "tokens=*" %%a in ('reg query HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall') do (
    for /f "tokens=2,* delims= " %%b in ('reg query %%a /v Publisher') do (
        IF "%%c" == "Skype Technologies S.A." (
            for /f "tokens=2,* delims= " %%d in ('reg query %%a /v UninstallString') do (
                echo %%e
            )
        )
    )

)

Is there a cleaner and safer way to achieve this in batch?

有没有更清洁、更安全的方法来批量实现这一目标?

回答by Oleg

It seems me more easy to use

好像我比较好用

reg QUERY HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall  /f "Skype Technologies S.A." /s

as a basis for your batch file. It produces a simple output like following

作为批处理文件的基础。它产生一个简单的输出,如下所示

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{D103C4BA-F905-437A-8049-DB24763BBE36}
    Publisher    REG_SZ    Skype Technologies S.A.

End of search: 1 match(es) found.

回答by Marcus Zheng

Here is a batch script which can extract Uninstall string e.g. MsiExec.exe /X[GUID]from x86/32bit registry keys as well as the software name. The output looks like this. You can add your logic e.g. a IF statement to filter and uninstall the software if you want to.

这是一个批处理脚本,它可以从 x86/32 位注册表项以及软件名称中提取卸载字符串,例如MsiExec.exe /X[GUID]。输出看起来像这样。如果需要,您可以添加逻辑,例如 IF 语句来过滤和卸载软件。

Note: Not all the applications have a MsiExec.exe /Xstring and some MsiExec.exestrings contain MsiExec.exe /Iwhich mean reinstall it. They will be replaced with MsiExec.exe /Xin the script

注意:并非所有应用程序都有MsiExec.exe /X字符串,某些MsiExec.exe字符串包含MsiExec.exe /I,这意味着重新安装它。它们将在脚本中替换为MsiExec.exe /X

Remove ^|findstr "MsiExec.exe"from the script below if you want to list ALL 32bit applications

如果要列出所有 32 位应用程序,请从下面的脚本中删除^|findstr "MsiExec.exe"

MsiExec.exe /X{87D50333-E534-493A-8E98-0A49BC28F64B}    SQL Server 2012 Database Engine Services
MsiExec.exe /X{C22613C2-C7A4-4761-A906-116ECD4E7477}    SQL Server 2012 Database Engine Services
MsiExec.exe /X{8BC3EEC9-090F-4C53-A8DA-1BEC913040F9}    Microsoft .NET Framework 4.6.1 Targeting Pack
MsiExec.exe /X{8C5FB518-E78C-F8F0-BFEC-8EAC65F51003}    Windows Assessment Services - Client (Client SKU)
MsiExec.exe /X{90160000-008C-0000-0000-0000000FF1CE}    Office 16 Click-to-Run Extensibility Component
MsiExec.exe /X{90160000-008C-0409-0000-0000000FF1CE}    Office 16 Click-to-Run Localization Component
MsiExec.exe /X{91361B2A-F741-E591-303B-4EF957F3BAF1}    Windows Assessment Toolkit (AMD64 Architecture Specific)


@echo off
setlocal ENABLEDELAYEDEXPANSION

set x86GUID=HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall
for /f "tokens=2*" %%A in (
  'reg query "%x86GUID%" /V /F DisplayName /S /E 2^>nul ^| findstr "DisplayName"'
) do (
  for /f "delims=" %%P in ('reg query "%x86GUID%" /s /f "%%B" 2^>nul ^| findstr "HKEY_LOCAL_MACHINE"') do (
    for /f "tokens=2*" %%X in (
      'reg query "%%P" /v "UninstallString" 2^>nul ^|findstr "UninstallString" ^|findstr "MsiExec.exe"'
    ) do (
      set MsiStr=%%Y
      set MsiStr=!MsiStr:/I=/X!
      echo !MsiStr! %%B
    )
  )
)