Solved

Batch file output

Posted on 2016-08-23
20
77 Views
Last Modified: 2016-08-25
Hi All,

I have a huge query to find the installed software and to display its version using registry keys.
I would like to know if anyone here can tell me how to redirect the output to a CSV file.
Also could you tell how I can add another parameter?

@ECHO OFF 
SETLOCAL EnableExtensions DisableDelayedExpansion
call :getsw "HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall"
call :getsw "HKCU\Software\Microsoft\Windows\CurrentVersion\Uninstall"
call :getsw "HKLM\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
ENDLOCAL
goto :eof

:getsw
for /F "delims=" %%g in ('reg query "%~1" 2^>NUL') do (
    set "_swRegKey=%%~g"
    set "_swName="
    set "_swVers="
	set "_swPub="

    for /F "tokens=1,2,*" %%G in ('
      reg query "%%~g" /V DisplayName 2^>NUL ^| findstr /I "DisplayName"
                                  ') do set "_swName=%%~I" 
    for /F "tokens=1,2,*" %%G in ('
      reg query "%%~g" /V DisplayVersion 2^>NUL ^| findstr /I "DisplayVersion"
                                  ') do set "_swVers=%%~I"
	for /F "tokens=1,2,*" %%G in ('
      reg query "%%~g" /V DisplayVersion 2^>NUL ^| findstr /I "Publisher"
                                  ') do set "_swPub=%%~I"

     
    call :echosw 
) 
goto :eof

:echosw
  SETLOCAL EnableDelayedExpansion
    if "%_swName%%_swVers%"=="" (
        echo unknown !_swRegKey!
    ) else (
        echo [!_swVers!] !_swName!
    )


  ENDLOCAL


goto :eof

echo >c:\drivers\final2.txt

exit

Open in new window

0
Comment
Question by:Siddharth S
  • 10
  • 4
  • 3
  • +1
20 Comments
 
LVL 51

Expert Comment

by:Bill Prew
ID: 41766774
What do you want the output CSV to look like, can you share a sample of a couple of lines?  Do you want a header line?

What do you mean by "add another parameter"?

~bp
0
 

Expert Comment

by:Emmanuel Ohue
ID: 41766778
Hi,

Impressive script.

is it for a list of pcs or do you run it on an individual basis?

Get-CimInstance -ClassName Win32_Product | select caption, version > c:\installedsoftware.txt   will give you the same result. you can then of course put that in a script and use the foreach loop
0
 

Expert Comment

by:Emmanuel Ohue
ID: 41766780
sorry amend the output to .csv rather
0
 

Author Comment

by:Siddharth S
ID: 41766782
Hi Bill,

Thanks for the quick response.

1) Yes I want the csv to have headers if possible, if not, that is okay as well.
    -The csv can be delimited by tab or comma or any other charector.
Planning to save the csv to  c:\drivers\%computername%.csv

The file displays the version followed by the name of the program.

2) This query looks into  registry entries. Say I want to add publisher as well, which is another entry in that registry, how can I incorporate that?
0
 

Author Comment

by:Siddharth S
ID: 41766784
Hi Emmanuel,

Thanks but use of wmi class only gives programs which have been installed using MSI.
The only way to get the non-MSI softwares is by digging into registries.
0
 

Expert Comment

by:Emmanuel Ohue
ID: 41766800
Opps Sorry. didn't think it through
0
 

Author Comment

by:Siddharth S
ID: 41766860
No problem, just looking for help :)
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 41766867
Okay, give this a try, may need a little more adjusting, but I think is headed in the direction you want.

@ECHO OFF 
SETLOCAL EnableExtensions EnableDelayedExpansion

set OutputFile=c:\drivers\%computername%.csv
echo "Key","Version","Name","Publisher">"%OutputFile%"

call :getsw "HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall"
call :getsw "HKCU\Software\Microsoft\Windows\CurrentVersion\Uninstall"
call :getsw "HKLM\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"

ENDLOCAL
exit /b

:getsw
    for /F "delims=" %%g in ('reg query "%~1" 2^>NUL') do (
        set "_swRegKey=%%~g"
        set "_swName="
        set "_swVers="
        set "_swPub="
       
        for /F "tokens=1,2,*" %%G in ('
          reg query "%%~g" /V DisplayName 2^>NUL ^| findstr /I "DisplayName"
                                      ') do set "_swName=%%~I" 
        for /F "tokens=1,2,*" %%G in ('
          reg query "%%~g" /V DisplayVersion 2^>NUL ^| findstr /I "DisplayVersion"
                                      ') do set "_swVers=%%~I"
        for /F "tokens=1,2,*" %%G in ('
          reg query "%%~g" /V Publisher 2^>NUL ^| findstr /I "Publisher"
                                      ') do set "_swPub=%%~I"
       
        call :echosw 
        ) 
    exit /b

:echosw
    if "!_swName!!_swVers!"=="" (
        echo "unknown","!_swRegKey!","","">>"%OutputFile%"
    ) else (
        echo "!_swRegKey!","!_swVers!","!_swName!","!_swPub!">>"%OutputFile%"
    )
    exit /b

Open in new window

~bp
0
 
LVL 83

Expert Comment

by:oBdA
ID: 41767122
This one drops the support for HKCU (which is usually empty anyway) in favor of remote support. You can (optionally) pass it a machine to query as first argument, default is to query the local machine.
It'll not list anything without DisplayName (these won't be listed in Control Panel anyway), and the column "SystemComponent" will show whether the software is considered a system component (and would normally be hidden in Control Panel).
@echo off
setlocal enabledelayedexpansion
if "%~1"=="" (
	set Computer=%ComputerName%&set UNC=
) else (
	set Computer=%~1&set UNC=\\!Computer!\
)
set CsvFile=C:\Temp\%Computer%.csv

>"%CsvFile%" echo "ComputerName","DisplayName","DisplayVersion","Publisher","SystemComponent","Architecture","Parent"
call :GetKey "HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall" "x64"
if errorlevel 1 exit /b 1
call :GetKey "HKLM\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall" "x86"
goto :eof

:GetKey
set Parent=
for /f "delims=" %%u in ('reg.exe query "%UNC%%~1" ^| find.exe /i "\Uninstall\"') do (
	set Parent=%%~nxu
	for %%c in (DisplayName Publisher DisplayVersion SystemComponent) do set %%c=
	for /f "tokens=1,2,*" %%a in ('reg.exe query "%UNC%%%u" ^| find.exe "REG_"') do (set %%a=%%c)
	if "!SystemComponent!"=="0x1" (set SystemComponent=True) else (set SystemComponent=False)
	if not "!DisplayName!"=="" (
		>>"%CsvFile%" echo "%Computer%","!DisplayName!","!DisplayVersion!","!Publisher!","!SystemComponent!","%~2","!Parent!"
	)
)
if "%Parent%"=="" exit /b 1

Open in new window

0
 

Author Comment

by:Siddharth S
ID: 41769787
Hi Bill

Amazing, thanks a lot.

I forgot to add 2 things. I need the %computername% as a column. Basically every row should have it.
Also I am unable to save the file with the same filename in a different location.

If you can help me achieve those, it will be great

But thanks a lot for your help already. This is amazing.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:Siddharth S
ID: 41770063
Also, is there a way to make this query a bit faster? Right now it takes like 20 seconds to run on my laptop.

Is there any way?
0
 
LVL 83

Assisted Solution

by:oBdA
oBdA earned 250 total points
ID: 41770226
Well, have you tried my version at https:#a41767122 yet? It uses two exe calls per key found instead of 6, and it already has the ComputerName column.
This is pimped some more and now about 4 times faster than the initial version.
@echo off
setlocal enabledelayedexpansion
if "%~1"=="" (
	set Computer=%ComputerName%&set UNC=
) else (
	set Computer=%~1&set UNC=\\!Computer!\
)
set CsvFile=C:\Temp\%Computer%_New.csv

>"%CsvFile%" echo "ComputerName","DisplayName","DisplayVersion","Publisher","SystemComponent","Architecture","Parent"
call :GetKey "HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall" "x64"
if errorlevel 1 exit /b 1
call :GetKey "HKLM\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall" "x86"
goto :eof

:GetKey
set DisplayName=
(	for /f "tokens=1,2* delims=\ " %%u in ('reg.exe query "%UNC%%~1" /s') do (
		if "%%u"=="HKEY_LOCAL_MACHINE" (
			if defined DisplayName (
				if "!SystemComponent!"=="0x1" (set SystemComponent=True) else (set SystemComponent=False)
				echo "%Computer%","!DisplayName!","!DisplayVersion!","!Publisher!","!SystemComponent!","%~2","!Parent!"
			)
			for %%a in (DisplayName Publisher DisplayVersion SystemComponent) do set %%a=
			set Parent=%%~nxw
		) else (
			set %%u=%%w
		)
	)
	if defined DisplayName (
		if "!SystemComponent!"=="0x1" (set SystemComponent=True) else (set SystemComponent=False)
		echo "%Computer%","!DisplayName!","!DisplayVersion!","!Publisher!","!SystemComponent!","%~2","!Parent!"
	)
) >>"%CsvFile%"
if not defined Parent exit /b 1

Open in new window


Edit: fixed testing leftover (csv export was commented).
1
 

Author Comment

by:Siddharth S
ID: 41770293
Hi OBDA,

I am sorry I didn't respond to you earlier.

Yes, I tried your code but somehow the csv file was blank when I opened it. :(
Is it just me?
0
 
LVL 83

Expert Comment

by:oBdA
ID: 41770300
The old one or the new one? For the new one, I just noticed (right before you posted) taht I forgot to put the export back in that I had commented out to see results in the console.
So with the new version, you should have seen the results in the console, and a csv file with only the header line.
I edited the comment, so you can just recopy from the page (or remove the REM that was in front of line 35, and join line 35 to the end of line 34, so that the end looks like the current script.
Sorry for the confusion.
0
 

Author Comment

by:Siddharth S
ID: 41770308
Also,

@Bill Prew

I am no longer concerned about the speed as I used a VB script to silently call the .bat file.
I just need the computer name as the output and I think I am all good to go.

And thanks, I just cant thank enough for the help you guys have provided.
0
 

Author Comment

by:Siddharth S
ID: 41770318
Hi OBDA,

Can you help me with Bill's code just to add the computer name please.
I think that will be of great help since I am almost done with everything else.

I hope you don't mind that.
0
 
LVL 51

Accepted Solution

by:
Bill Prew earned 250 total points
ID: 41770323
Added computer name as first column.

@ECHO OFF 
SETLOCAL EnableExtensions EnableDelayedExpansion

set OutputFile=c:\drivers\%computername%.csv
echo "Computer","Key","Version","Name","Publisher">"%OutputFile%"

call :getsw "HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall"
call :getsw "HKCU\Software\Microsoft\Windows\CurrentVersion\Uninstall"
call :getsw "HKLM\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"

ENDLOCAL
exit /b

:getsw
    for /F "delims=" %%g in ('reg query "%~1" 2^>NUL') do (
        set "_swRegKey=%%~g"
        set "_swName="
        set "_swVers="
        set "_swPub="
       
        for /F "tokens=1,2,*" %%G in ('
          reg query "%%~g" /V DisplayName 2^>NUL ^| findstr /I "DisplayName"
                                      ') do set "_swName=%%~I" 
        for /F "tokens=1,2,*" %%G in ('
          reg query "%%~g" /V DisplayVersion 2^>NUL ^| findstr /I "DisplayVersion"
                                      ') do set "_swVers=%%~I"
        for /F "tokens=1,2,*" %%G in ('
          reg query "%%~g" /V Publisher 2^>NUL ^| findstr /I "Publisher"
                                      ') do set "_swPub=%%~I"
       
        call :echosw 
        ) 
    exit /b

:echosw
    if "!_swName!!_swVers!"=="" (
        echo "%COMPUTERNAME%","unknown","!_swRegKey!","","">>"%OutputFile%"
    ) else (
        echo "%COMPUTERNAME%","!_swRegKey!","!_swVers!","!_swName!","!_swPub!">>"%OutputFile%"
    )
    exit /b

Open in new window

~bp
1
 

Author Comment

by:Siddharth S
ID: 41770429
Amazing Bill.

Thanks ObDA.

I came for one solution but actually found 2. This is just amazing.
0
 

Author Closing Comment

by:Siddharth S
ID: 41770433
Amazing help from both.
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 41770440
Welcome.

~bp
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
wordlen challenge 3 47
Combine DSQuery result into loop script 6 39
How do I delete 2 31
index Out OF Range Exception error 4 31
A short article about problems I had with the new location API and permissions in Marshmallow
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now