Solved

Command to modify Registry entry

Posted on 2016-10-31
5
97 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 24

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Copy dir and files with robocopy 2 35
function opinion 9 34
Update Access FrontEnd by Version # 9 23
Matching items in two arrays 3 8
Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
A quick guide on how to use Group Policy to create a custom power plan and set it active on Windows 7.
This Micro Tutorial will give you a basic overview of Windows Live Photo Gallery and show you various editing filters and touches to photos you can apply. This will be demonstrated using Windows Live Photo Gallery on Windows 7 operating system.
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…

920 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

13 Experts available now in Live!

Get 1:1 Help Now