I just remembered that I had to do this another way because .RemoveNetworkDrive only disconnects the drive but the mapping still exists - thus, new mappings with the same drive letters can't be created. In that case, you'll need to remove them from the users hive.
Open regedit.exe and drill down to HKEY_CURRENT_USER\Network. This is the key where mapped drives are stored (on a user basis). Each mapping exists as it's own key with values that define the mapping. The script will need to delete these key entries to delete a mapping.
The drives are stored in the HKEY_CURRENT_USER\Network key which is not accessible thru conventional means (objShell) unless you know the users SID (HKEY_USERS + SID = HKEY_CURRENT_USER). Therefore, you need to use the standard registry provider (objReg) to access and remove the reg key.
1. Connent to the Standard Registry Provider
2. Enumberate Drive mappings
3. Delete all drives that <> Z:
Code:
CONST HKCU = &H80000001
strComputer = "." 'Local Computer
set objNetwork = CreateObject("WScript.Network") 'Create Network object
'1. Connent to the Standard Registry Provider
set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
'2. Enumberate Drive mappings
set colShares = objNetwork.EnumNetworkDrives()
'3. Delete all drives that <> Z:
for i = 0 To colShares.Count - 1 step 2
if (colShares.item(i) <> "Z:") then
'Because the item value is letter-colon, we take the first left character of the item
objReg.DeleteKey HKCU, "Nework\" & left(colShares.item(i), 1)
end if
next
Great registry managment code examples
-Geates
"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live."
- Martin Golding
"There are seldom good technological solutions to behavioral problems."
- Ed Crowley, Exchange guru and technology curmudgeon