Siddharth S
asked on
Batch file output
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?
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
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
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
sorry amend the output to .csv rather
ASKER
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?
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%.
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?
ASKER
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.
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.
Opps Sorry. didn't think it through
ASKER
No problem, just looking for help :)
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
~bp
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).
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
ASKER
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.
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.
ASKER
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?
Is there any way?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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?
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?
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.
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.
ASKER
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.
@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.
ASKER
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Amazing Bill.
Thanks ObDA.
I came for one solution but actually found 2. This is just amazing.
Thanks ObDA.
I came for one solution but actually found 2. This is just amazing.
ASKER
Amazing help from both.
Welcome.
~bp
~bp
What do you mean by "add another parameter"?
~bp