Yan
Yan

Reputation: 592

Output multiple registry values per key

I want to output both DisplayName and DisplayVersion of each program installed.

for /f "tokens=2*" %a in (
  'reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" /s ^
  | findstr /c:"DisplayName" /c:"DisplayVersion"'
) do @echo %b

It does output both of them one per line but I want to display them on one line, how would you do that?

> DisplayName, DisplayVersion

Upvotes: 0

Views: 1099

Answers (2)

Compo
Compo

Reputation: 38622

The easiest method would be to just output the information directly within Windows PowerShell but that would be directly contrary to the tags you've applied to this question.

Here therefore is a batch file which uses Powershell:

@Echo Off
Set "KP=Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"

PowerShell -C "GP HKLM:\%KP%\*|Select DisplayName,DisplayVersion|FT -A -H"
Pause

You may remove \Wow6432Node from line 2 if you're not using this on a 64bit Operating System.


It is possible for the DisplayName output to be truncated due to their character lengths and cmd.exe's buffersize. This can be worked around using the following, (possibly crude), code:

@Echo Off
Set "KP=Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
Set/A W=90,H=120

PowerShell -C "&{$H=Get-Host;$R=$H.UI.RawUI;$B=$R.BufferSize;"^
 "$W=$R.WindowSize;$B.Width=If (%W% -GT $W.Width) {%W%} Else {$W.Width};"^
 "$B.Height=If (%H% -GT $W.Height) {%H%} Else {$W.height};$R.BufferSize=$B};"^
 "GP HKLM:\%KP%\*|Select DisplayName,DisplayVersion|FT -A -H"
Pause

In the above code you can adjust that height/width on line 3 as required, this may be necessary if you have some very long DisplayName's or a huge list of installed software under that key.

Upvotes: 0

dbenham
dbenham

Reputation: 130849

You probably should check both 32bit and 64bit registries. If I don't specify which one, then my REG QUERY searches only 64bit by default.

Not all program keys have DisplayName and/or DisplayVersion.

The code below lists the full key if DisplayName is not present, and lists an empty version if DisplayVersion is not present. Both 32bit and 64bit registries are searched.

@echo off
setlocal enableDelayedExpansion
set "key="
set "name="
set "ver="
for %%s in (32 64) do (
  for /f "delims=" %%A in ('reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" /s /reg:%%s 2^>nul') do (
    set "ln=%%A"
    if "!ln:~0,4!" equ "HKEY" (
      if defined name (echo "!name!","!ver!") else if defined key echo "!key!","!ver!"
      set "name="
      set "ver="
      set "key=%%A"
    ) else for /f "tokens=1,2*" %%A in ("!ln!") do (
      if "%%A" equ "DisplayName" set "name=%%C"
      if "%%A" equ "DisplayVersion" set "ver=%%C"
    )
  )
)
if defined name (echo "!name!","!ver!") else if defined key echo "!key!","!ver!"

Upvotes: 1

Related Questions