Inserting information found in a batch script into the registry

795 views Asked by At

I have a .reg file that inserts some info into the System properties. I would like to add the computer model to this also. This is the current script bellow. I would like to insert the Manufacturer and the model number in here automatically if possible.

the model and manufacturer can be found using command prompt with the following commands

wmic computersystem get model
wmic computersystem get manufacturer

i would like to know how i can automatically insert the results into the .reg file bellow so that the script will work for different models ect.

Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\OEMInformation]
"Logo"="c:\\OEM\\OEMLOGO.BMP"
"Manufacturer"="example"
"Model"="example"
"SupportHours"="8:30am - 5:00pm"
"SupportPhone"="0458228893"
"SupportURL"="www.web.com.au"
1

There are 1 answers

0
rojo On

In general, when you want to capture and work with the output of a command, use a for /F loop.

for /f "delims=" %%I in ('command') do set "var=%%~I"

... or similar. However, WMI queries are a little tricky, in that results are displayed in an odd encoding (UCS-2 LE, if I recall correctly); so capturing the output of wmic requires a little more care.

One trick is to query an extra column, then ignore it. Then the columns you want aren't affected by the tricksy encoding. In this case,

for /f "tokens=2-3 delims=," %%I in (
    'wmic computersystem get manufacturer^,model^,status /format:csv'
) do (
    set "manufacturer=%%~I"
    set "model=%%~J"
)

... should get you the values you need into variables you can use. Since column 1 is your computer name and column 4 is the "Status" column, you can just drop those and end up with your intended values uninfluenced by any beginning-of-line or end-of-line encoding quirks.

From there, it's just a simple matter of

set "branch=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\OEMInformation"
reg add "%branch%" /v Model /d "%model%" /f
reg add "%branch%" /v Manufacturer /d "%manufacturer%" /f

or

>>regfile.reg echo "Model"="%model%"
>>regfile.reg echo "Manufacturer"="%manufacturer%"