Solved

Command to modify Registry entry

Posted on 2016-10-31
5
81 Views
Last Modified: 2016-11-07
I am looking to modify some workstation registries via a batch script.

What I would like to do is modify the the DevicePath of where to search for drivers.

Presently, HKLM\Software\Microsoft\Windows\Current Version has a key named DevicePath with a type REG_EXPAND_SZ

I would like to append to the existing data c:\drivers

Anyone have a method to accomplish this?

edit HKLM
0
Comment
Question by:nappy_d
5 Comments
 
LVL 23

Expert Comment

by:Dirk Kotte
ID: 41867986
depends from script / shell / app you wish to use...
tell us more about your environment and possible the solution is more specific.
because you wish to change a HLM reg-key, your script has to run with administrator-privileges.

some examples:

from command shell:
c:\>reg /?   (reg query / reg add / ...)
http://ss64.com/nt/reg.html

powershell:
https://blogs.technet.microsoft.com/heyscriptingguy/2012/05/12/weekend-scripter-use-powershell-to-easily-modify-registry-property-values/
https://msdn.microsoft.com/en-us/powershell/scripting/getting-started/cookbooks/working-with-registry-keys

vbs:
https://msdn.microsoft.com/en-us/library/aa384906(v=vs.85).aspx
http://ss64.com/vb/regwrite.html
0
 
LVL 19

Accepted Solution

by:
deroode earned 500 total points
ID: 41868015
If you just want to add C:\Drivers to the existing path (which is default %Systemroot%\inf) you can get away with just a REG command:

REG add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion /v DevicePath /d %Systemroot%\inf;C:\Drivers /f

Open in new window


If however you expect that your workstations may have different values for the DevicePath entry, you want to append the C:\Drivers to the existing value:
set append=;C:\Drivers
set key=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion
set value=DevicePath
set oldVal=

for /F "skip=2 tokens=3" %%r in ('reg query %key% /v %value%') do set oldVal=%%r
echo previous=%oldVal%

set newVal=%oldVal%%append% 

reg add %key% /v %value% /d %newVal% /f

Open in new window

2
 
LVL 23

Expert Comment

by:Coralon
ID: 41868467
This should be pretty simple..

reg add "HKLM\Software\Microsoft\Windows\Current Version" /v DevicePath /d ^%SYSTEMROOT^%\inf;^%systemdrive^%\drivers; /t REG_EXPAND_SZ /f

Open in new window


The key is to escape the % symbols with the carets (^).. otherwise the values will be translated and you'll lose the benefit of REG_EXPAND_SZ.  (You can put in c:\drivers instead of ^%systemdrive^%\Drivers if you want to hard code it)

Coralon
0
 
LVL 32

Author Comment

by:nappy_d
ID: 41868833
Thanks @Deroode.  That is just what I was looking for in the second option you provided.

Currently testing this and will follow-up :)
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 41876163
In PowerShell:
$key, $name, $addvalue = 'HKLM:\Software\Microsoft\Windows\CurrentVersion', 'DevicePath', ';C:\Drivers\'
$value = (Get-ItemProperty $key).$name
if (';'+$value+';' -notlike "*$addvalue;*") { Set-ItemProperty $key $name -value ($value + $addvalue) }

Open in new window

This also checks that the path is not added already.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
This article will help you understand what HashTables are and how to use them in PowerShell.
This Micro Tutorial will go in depth within Systems and Security in Windows 7 and will go into detail regarding Action Center, Windows Firewall, System, etc. This will be demonstrated using Windows 7 operating system.
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.

708 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

19 Experts available now in Live!

Get 1:1 Help Now