VBscript is great for most stuff, but occasionally you try and do something and it can’t handle it. I came across another one of these yesterday, trying to remove some old network printer connections. I ended up having to try and delete some registry values, one of which was located in the following key:
HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Devices
The value name was of the form of the UNC path to the shared printer:
\\printserver\printer
Now, using oShell.RegDelete you don’t specify the value as a separate parameter to the key, you just concatenate the two together, which means I ended up doing:
oShell.RegDelete "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Devices\\\printserver\printer"
Which a) looks wrong and b) doesn’t work. You get an error:
-2147024894 Invalid root in registry key "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Devices\\\printserver\printer"
I actually had the reg key and value name in variables, but the end result was the above. I tried delimiting the backslashes with more backslashes, a bit like in .reg files, but no. Tried putting quotes around the value name, but that still looks wrong and still didn’t work.
So in the end I wrote a function to do it using WMI, which I tend to avoid if possible, but in this case it didn’t appear to be possible.
' These constants are required by WMI to specify the registry root
Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003
Function regDeleteWMI(iRegRoot, sRegPath, sRegValue)
Dim oReg
regDeleteWMI = False
On Error Resume Next
Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
oReg.DeleteValue iRegRoot, sRegPath, sRegValue
If Err.Number <> 0 Then
WScript.Echo "Error: regDeleteWMI: ", iRegRoot, sRegPath, sRegValue, Err.Number, Err.Description
Else
regDeleteWMI = True
End If
End Function
Usage is as follows:
If regDeleteWMI(HKEY_CURRENT_USER,"Software\Microsoft\Windows NT\CurrentVersion\Devices","\\printserver\printer") Then 'deleted ok End If