-
Notifications
You must be signed in to change notification settings - Fork 8
/
RegUpdateAllHkcuHkcr.vbs
499 lines (415 loc) · 18.1 KB
/
RegUpdateAllHkcuHkcr.vbs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
On Error Resume Next
'
' Option Explicit
' AUTHOR: Mick Grove
' http://micksmix.wordpress.com
'
' Tested and works on Windows XP and Windows 7 (x64)
' Should work fine on Windows 2000 and newer OS'
'
' Script name: RegUpdateAllHkcuHkcr.vbs
' Run with cscript to suppress dialogs: cscript.exe RegUpdateAllHkcuHkcr.vbs
'
' CHANGELOG:
'
' 11/15/13 - Now able to update NTUSER.DAT and/or USRCLASS.DAT (HKCU and/or HKCR)
' 8/25/13 - Added ability to delete keys
' 4/23/13 - Added ability to write REG_BINARY values
' 4/11/13 - Fixed bug where it wouldn't work when run by SYSTEM account
' 3/28/13 - Huge code cleanup and bug fixes
' 1/13/12 - Initial release
'
'
Dim WshShell, RegRoot, objFSO
Set WshShell = CreateObject("WScript.shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
'
Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003
Const HKEY_CURRENT_CONFIG = &H80000005
'
Const DAT_NTUSER = &H70000000
Const DAT_USRCLASS = &H70000001
'==============================================
' SCRIPT BEGINS HERE
'==============================================
'
'This is where our HKCU is temporarily loaded, and where we need to write to it
RegRoot = "HKLM\TEMPHIVE" ' You don't really need to change this, but you can if you want
'== Loads each user's "HKCU" registry hive
Call Load_Registry_For_Each_User(DAT_NTUSER)
'== Loads each user's "HKCR" registry hive
Call Load_Registry_For_Each_User(DAT_USRCLASS)
WScript.Echo vbCrLf & "Processing complete!"
WScript.Quit(0)
' |
' |
'====================================================================
Sub KeysToModify(sRegistryRootToUse, DAT_FILE)
'==============================================
' Change variables here, or add additional keys
'==============================================
'
On Error Resume Next
If DAT_FILE = DAT_NTUSER Then 'This is for updating HKCU keys
Dim strRegPathParent01
Dim strRegPathParent02
Dim strRegPathParent03
strRegPathParent01 = "Software\Microsoft\Windows\CurrentVersion\Internet Settings"
strRegPathParent02 = "Software\Microsoft\Internet Explorer\Main"
strRegPathParent03 = "Software\_Test\MyTestBinarySubkey"
WshShell.RegWrite sRegistryRootToUse & "\" & strRegPathParent01 & "\DisablePasswordCaching", "00000001", "REG_DWORD"
WshShell.RegWrite sRegistryRootToUse & "\" & strRegPathParent02 & "\FormSuggest PW Ask", "no", "REG_SZ"
'===
'REG_BINARY values are special
'===
'
' 1st step is to create subkey path
WshShell.RegWrite sRegistryRootToUse & "\" & strRegPathParent03 & "\", ""
SetBinaryRegKeys sRegistryRootToUse, strRegPathParent03, "My Test Binary Value","hex:23,00,41,00,43,00,42,00,6c,00"
'
' You can add additional registry keys to write here if you would like
'
'=======================
' DELETING KEYS
'=======================
'
' This will RECURSIVELY delete the parent reg key and all items below it.
' USE CAUTION!
'
Dim sSubkeyPathToDelete
sSubkeyPathToDelete = "Software\_Test"
'
Call DeleteSubkeysRecursively(sRegistryRootToUse, sSubkeyPathToDelete) ' recursively deletes the binary reg key we added earlier
'
'
' This will delete just a single value
Call DeleteSingleValue(sRegistryRootToUse, strRegPathParent02, "FormSuggest PW Ask") ' deletes the 'FormSuggest PW Ask' key set earlier
'
ElseIf DAT_FILE = DAT_USRCLASS Then ' This is for updating HKCR keys per-user
Dim sHkcrParent01
'sHkcrParent01 = "Software\Microsoft\MediaPlayer\Preferences"
sHkcrParent01 = "FirefoxURL"
WshShell.RegWrite sRegistryRootToUse & "\" & sHkcrParent01 & "\FriendlyTypeName", "Firefox URL", "REG_SZ"
End If
End Sub
'
'
'
'
'
'
'
' NO CHANGES NECESSARY BELOW THIS LINE
'
'
'
'
'
'
'
'
'
Sub DeleteSingleValue(RegRoot, strRegistryKey, strValue)
On Error Resume Next
If Left(strRegistryKey,1) = "\" Then
strRegistryKey = Mid(strRegistryKey, 2)
End If
WshShell.Run "reg.exe delete " & chr(34) & RegRoot & "\" & strRegistryKey & chr(34) & " /v " & chr(34) & strValue & chr(34) & " /f", 0, True
End Sub
Sub DeleteSubkeysRecursively(RegRoot, strRegistryKey)
On Error Resume Next
'
' BE VERY CAREFUL CALLING THIS SUB
'
' This will RECURSIVELY delete the requested path...meaning
' it will delete the path and everything beneath it!
'
' This action cannot be undone!
'
If Left(strRegistryKey,1) = "\" Then
strRegistryKey = Mid(strRegistryKey, 2)
End If
WshShell.Run "reg.exe delete " & chr(34) & RegRoot & "\" & strRegistryKey & chr(34) & " /f", 0, True
'wscript.echo "reg.exe delete " & chr(34) & RegRoot & "\" & strRegistryKey & chr(34) & " /f"
End Sub
Function SetBinaryRegKeys(sRegistryRootToUse, strRegPathParent, sKeyName, sHexString)
On Error Resume Next
Dim sBinRegRoot
Dim sBinRegPartialPath
Dim arrBinRegRoot
arrBinRegRoot = GetRegRootToUseForBinaryValues(sRegistryRootToUse)
sBinRegRoot = arrBinRegRoot(0)
sBinRegPartialPath = arrBinRegRoot(1)
If Len(sBinRegPartialPath) > 0 Then
sBinRegPartialPath = sBinRegPartialPath & "\"
End If
WriteBinaryValue sBinRegRoot, sBinRegPartialPath & strRegPathParent, sKeyName, sHexString
End Function
Function WriteBinaryValue(RegHive, strKeyPath, strValueName, strHexValues)
On Error Resume Next
Dim objRegistry
Dim arrHexValues, arrDecValues
Set objRegistry = GetObject("Winmgmts:root\default:StdRegProv")
'Example: strHexData = "hex:23,00,41,00,43,00,42,00,6c,00"
arrHexValues = Split(Replace(strHexValues, "hex:", ""), ",")
arrDecValues = DecimalNumbers(arrHexValues)
Dim iResult
iResult = objRegistry.SetBinaryValue (RegHive, _
strKeyPath, strValueName, arrDecValues)
If (iResult = 0) Then
'Wscript.Echo "Binary value added successfully"
Else
Wscript.Echo "*** Error adding binary value at " & strKeyPath & "\" & strValueName
End If
End Function
Function GetRegRootToUseForBinaryValues(sRegRoot)
On Error Resume Next
Dim sNewRoot
Dim sPartialPath
sRegRoot = UCase(sRegRoot)
'HKEY_CURRENT_USER
'
If Left(sRegRoot,Len("HKCU\")) = "HKCU\" Then
sNewRoot = HKEY_CURRENT_USER
sPartialPath = Replace(sRegRoot,"HKCU\",1, Len("HKCU\") + 1)
ElseIf Left(sRegRoot,Len("HKCU")) = "HKCU" Then
sNewRoot = HKEY_CURRENT_USER
sPartialPath = Replace(sRegRoot,"HKCU",1, Len("HKCU") + 1)
ElseIf Left(sRegRoot,Len("HKEY_CURRENT_USER\")) = "HKEY_CURRENT_USER\" Then
sNewRoot = HKEY_CURRENT_USER
sPartialPath = Replace(sRegRoot,"HKEY_CURRENT_USER\",1, Len("HKEY_CURRENT_USER\") + 1)
ElseIf Left(sRegRoot,Len("HKEY_CURRENT_USER")) = "HKEY_CURRENT_USER" Then
sNewRoot = HKEY_CURRENT_USER
sPartialPath = Replace(sRegRoot,"HKEY_CURRENT_USER",1, Len("HKEY_CURRENT_USER") + 1)
'HKEY_LOCAL_MACHINE
'
ElseIf Left(sRegRoot,Len("HKLM\")) = "HKLM\" Then
sNewRoot = HKEY_LOCAL_MACHINE
sPartialPath = Replace(sRegRoot,"HKLM\",1, Len("HKLM\") + 1)
ElseIf Left(sRegRoot,Len("HKLM")) = "HKLM" Then
sNewRoot = HKEY_LOCAL_MACHINE
sPartialPath = Replace(sRegRoot,"HKLM",1, Len("HKLM") + 1)
ElseIf Left(sRegRoot,Len("HKEY_LOCAL_MACHINE\")) = "HKEY_LOCAL_MACHINE\" Then
sNewRoot = HKEY_LOCAL_MACHINE
sPartialPath = Replace(sRegRoot,"HKEY_LOCAL_MACHINE\",1, Len("HKEY_LOCAL_MACHINE\") + 1)
ElseIf Left(sRegRoot,Len("HKEY_LOCAL_MACHINE")) = "HKEY_LOCAL_MACHINE" Then
sNewRoot = HKEY_LOCAL_MACHINE
sPartialPath = Replace(sRegRoot,"HKEY_LOCAL_MACHINE",1, Len("HKEY_LOCAL_MACHINE") + 1)
'HKEY_USERS
'
ElseIf Left(sRegRoot,Len("HKEY_USERS\")) = "HKEY_USERS\" Then
sNewRoot = HKEY_USERS
sPartialPath = Replace(sRegRoot,"HKEY_USERS\",1, Len("HKEY_USERS\") + 1)
ElseIf Left(sRegRoot,Len("HKEY_USERS")) = "HKEY_USERS" Then
sNewRoot = HKEY_CURRENT_MACHINE
sPartialPath = Replace(sRegRoot,"HKEY_USERS",1, Len("HKEY_USERS") + 1)
'HKEY_CLASSES_ROOT
'
ElseIf Left(sRegRoot,Len("HKEY_CLASSES_ROOT\")) = "HKEY_CLASSES_ROOT\" Then
sNewRoot = HKEY_CLASSES_ROOT
sPartialPath = Replace(sRegRoot,"HKEY_CLASSES_ROOT\",1, Len("HKEY_CLASSES_ROOT\") + 1)
ElseIf Left(sRegRoot,Len("HKEY_CLASSES_ROOT")) = "HKEY_CLASSES_ROOT" Then
sNewRoot = HKEY_CURRENT_MACHINE
sPartialPath = Replace(sRegRoot,"HKEY_CLASSES_ROOT",1, Len("HKEY_CLASSES_ROOT") + 1)
'HKEY_CURRENT_CONFIG
'
ElseIf Left(sRegRoot,Len("HKEY_CURRENT_CONFIG\")) = "HKEY_CURRENT_CONFIG\" Then
sNewRoot = HKEY_CURRENT_CONFIG
sPartialPath = Replace(sRegRoot,"HKEY_CURRENT_CONFIG\",1, Len("HKEY_CURRENT_CONFIG\") + 1)
ElseIf Left(sRegRoot,Len("HKEY_CURRENT_CONFIG")) = "HKEY_CURRENT_CONFIG" Then
sNewRoot = HKEY_CURRENT_MACHINE
sPartialPath = Replace(sRegRoot,"HKEY_CURRENT_CONFIG",1, Len("HKEY_CURRENT_CONFIG") + 1)
End If
GetRegRootToUseForBinaryValues = Array(sNewRoot,sPartialPath)
End Function
Function DecimalNumbers(arrHex)
On Error Resume Next
' from: http://www.petri.co.il/forums/showthread.php?t=46158
Dim i, strDecValues
For i = 0 to Ubound(arrHex)
If isEmpty(strDecValues) Then
strDecValues = CLng("&H" & arrHex(i))
Else
strDecValues = strDecValues & "," & CLng("&H" & arrHex(i))
End If
Next
DecimalNumbers = split(strDecValues, ",")
End Function
Function GetDefaultUserPath
On Error Resume Next
Dim objRegistry
Dim strKeyPath
Dim strDefaultUser
Dim strDefaultPath
Dim strResult
Set objRegistry = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
strKeyPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"
objRegistry.GetExpandedStringValue HKEY_LOCAL_MACHINE,strKeyPath,"DefaultUserProfile",strDefaultUser
objRegistry.GetExpandedStringValue HKEY_LOCAL_MACHINE,strKeyPath,"ProfilesDirectory",strDefaultPath
If Len(strDefaultUser) < 1 or IsEmpty(strDefaultUser) or IsNull(strDefaultUser) Then
'must be on Vista or newer
objRegistry.GetExpandedStringValue HKEY_LOCAL_MACHINE,strKeyPath,"Default",strDefaultPath
strResult = strDefaultPath
Else
'must be on XP
strResult = strDefaultPath & "\" & strDefaultUser
End If
GetDefaultUserPath = strResult
End Function
Function RetrieveUsernameFromPath(sTheProfilePath)
On Error Resume Next
Dim lstPath
Dim sTmp
Dim sUsername
lstPath = Split(sTheProfilePath,"\")
For each sTmp in lstPath
sUsername = sTmp
'last split is our username
Next
RetrieveUsernameFromPath = sUsername
End Function
Sub LoadProfileHive(sProfileDatFilePath, sCurrentUser, DAT_FILE)
On Error Resume Next
Dim intResultLoad, intResultUnload, sUserSID
'Load user's HKCU into temp area under HKLM
intResultLoad = WshShell.Run("reg.exe load " & RegRoot & " " & chr(34) & sProfileDatFilePath & chr(34), 0, True)
If intResultLoad <> 0 Then
' This profile appears to already be loaded...lets update it under the HKEY_USERS hive
Dim objRegistry2, objSubKey2
Dim strKeyPath2, strValueName2, strValue2
Dim strSubPath2, arrSubKeys2
Set objRegistry2 = GetObject("winmgmts:\\.\root\default:StdRegProv")
strKeyPath2 = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"
objRegistry2.EnumKey HKEY_LOCAL_MACHINE, strKeyPath2, arrSubkeys2
sUserSID = ""
For Each objSubkey2 In arrSubkeys2
strValueName2 = "ProfileImagePath"
strSubPath2 = strKeyPath2 & "\" & objSubkey2
objRegistry2.GetExpandedStringValue HKEY_LOCAL_MACHINE,strSubPath2,strValueName2,strValue2
If Right(UCase(strValue2),Len(sCurrentUser)+1) = "\" & UCase(sCurrentUser) Then
'this is the one we want
sUserSID = objSubkey2
End If
Next
If Len(sUserSID) > 1 Then
WScript.Echo " Updating another logged-on user: " & sCurrentUser & vbCrLf
If DAT_FILE = DAT_NTUSER Then
Call KeysToModify("HKEY_USERS\" & sUserSID, DAT_FILE)
ElseIf DAT_FILE = DAT_USRCLASS Then
Call KeysToModify("HKEY_USERS\" & sUserSID & "_Classes", DAT_FILE)
End If
Else
WScript.Echo(" *** An error occurred while loading HKCU for this user: " & sCurrentUser)
End If
Else
WScript.Echo(" HKCU loaded for this user: " & sCurrentUser)
End If
''
If sUserSID = "" then 'check to see if we just updated this user b/c they are already logged on
Call KeysToModify(RegRoot, DAT_FILE) ' update registry settings for this selected user
End If
''
If sUserSID = "" then 'check to see if we just updated this user b/c they are already logged on
intResultUnload = WshShell.Run("reg.exe unload " & RegRoot,0, True) 'Unload HKCU from HKLM
If intResultUnload <> 0 Then
WScript.Echo(" *** An error occurred while unloading HKCU for this user: " & sCurrentUser & vbCrLf)
Else
WScript.Echo(" HKCU UN-loaded for this user: " & sCurrentUser & vbCrLf)
End If
End If
End Sub
Function GetUserRunningScript()
On Error Resume Next
Dim sUserRunningScript, sComputerName
sUserRunningScript = WshShell.ExpandEnvironmentStrings("%USERNAME%") 'Holds name of current logged on user running this script
sComputerName = UCase(WshShell.ExpandEnvironmentStrings("%COMPUTERNAME%"))
If sUserRunningScript = "%USERNAME%" or sUserRunningScript = sComputerName & "$" Then
' This script might be run by the SYSTEM account or a service account
Dim sTheProfilePath
sTheProfilePath = WshShell.ExpandEnvironmentStrings("%USERPROFILE%") 'Holds name of current logged on user running this script
sUserRunningScript = RetrieveUsernameFromPath(sTheProfilePath)
End If
GetUserRunningScript = sUserRunningScript
End Function
Function RemoveTrailingPathDelimiter(sPath)
On Error Resume Next
Dim sUpdatedPath
sUpdatedPath = sPath
If Right(sUpdatedPath,1) = "\" Then
sUpdatedPath = Left(sUpdatedPath,Len(sUpdatedPath)-1)
End If
RemoveTrailingPathDelimiter = sUpdatedPath
End Function
Function GetPathToDatFileToUpdate(sProfilePath, DAT_FILE)
On Error Resume Next
Dim sDatFile, sPathToDat, sTrimmedProfilePath
Dim bFoundDatFile
sPathToDat = "" 'default
sTrimmedProfilePath = RemoveTrailingPathDelimiter(sProfilePath)
If DAT_FILE = DAT_NTUSER Then
sDatFile = "NTUSER.DAT"
If objFSO.FileExists(sTrimmedProfilePath & "\" & sDatFile) or objFSO.FileExists(chr(34) & sTrimmedProfilePath & "\" & sDatFile & chr(34)) Then
sPathToDat = sTrimmedProfilePath & "\" & sDatFile
End If
ElseIf DAT_FILE = DAT_USRCLASS Then
sDatFile = "USRCLASS.DAT"
If objFSO.FileExists(sTrimmedProfilePath & "\AppData\Local\Microsoft\Windows\" & sDatFile) OR _
objFSO.FileExists(chr(34) & sTrimmedProfilePath & "\AppData\Local\Microsoft\Windows\" & sDatFile & chr(34)) Then
sPathToDat = sTrimmedProfilePath & "\AppData\Local\Microsoft\Windows\" & sDatFile
ElseIf objFSO.FileExists(sTrimmedProfilePath & "\Local Settings\Application Data\Microsoft\Windows\" & sDatFile) OR _
objFSO.FileExists(chr(34) & sTrimmedProfilePath & "\Local Settings\Application Data\Microsoft\Windows\" & sDatFile & chr(34)) Then
sPathToDat = sTrimmedProfilePath & "\Local Settings\Application Data\Microsoft\Windows\" & sDatFile
End If
End If
GetPathToDatFileToUpdate = sPathToDat
End Function
Sub Load_Registry_For_Each_User(DAT_FILE)
On Error Resume Next
Dim sUserRunningScript
Dim objRegistry, objSubkey
Dim strKeyPath, strValueName, strValue, strSubPath, arrSubKeys
Dim sCurrentUser, sProfilePath, sNewUserProfile
Dim sPathToDatFile
sUserRunningScript = GetUserRunningScript
WScript.Echo "Updating the logged-on user: " & sUserRunningScript & vbCrLf
''
If DAT_FILE = DAT_NTUSER Then
Call KeysToModify("HKCU", DAT_FILE) 'Update registry settings for the user running the script
ElseIf DAT_FILE = DAT_USRCLASS Then
Call KeysToModify("HKCR", DAT_FILE) 'Update registry settings for the user running the script
End If
''
sNewUserProfile = GetDefaultUserPath
sPathToDatFile = GetPathToDatFileToUpdate(sNewUserProfile, DAT_FILE)
If Len(sPathToDatFile) > 0 Then
WScript.Echo "Updating the DEFAULT user profile which affects newly created profiles." & vbCrLf
Call LoadProfileHive(sPathToDatFile, "Default User Profile", DAT_FILE)
Else
WScript.Echo "Unable to update the DEFAULT user profile, because it could not be found at: " _
& vbCrLf & sPathToDatFile & vbCrLf
End If
Set objRegistry = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
strKeyPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"
objRegistry.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubkeys
For Each objSubkey In arrSubkeys
strValueName = "ProfileImagePath"
strSubPath = strKeyPath & "\" & objSubkey
objRegistry.GetExpandedStringValue HKEY_LOCAL_MACHINE,strSubPath,strValueName,strValue
sProfilePath = strValue
sCurrentUser = RetrieveUsernameFromPath(strValue)
If ((UCase(sCurrentUser) <> "ALL USERS") and _
(UCase(sCurrentUser) <> UCase(sUserRunningScript)) and _
(UCase(sCurrentUser) <> "LOCALSERVICE") and _
(UCase(sCurrentUser) <> "SYSTEMPROFILE") and _
(UCase(sCurrentUser) <> "NETWORKSERVICE")) then
sPathToDatFile = GetPathToDatFileToUpdate(sProfilePath, DAT_FILE)
If Len(sPathToDatFile) > 0 Then
WScript.Echo "Preparing to update the user: " & sCurrentUser
Call LoadProfileHive(sPathToDatFile, sCurrentUser, DAT_FILE)
End If
End If
Next
End Sub