Disconnect already existing Mapped Drives using VB or VBScript

habbotha used Ask the Experts™
Hi there
I need to create a script that Disconnects all Mapped Drives on the users PC.  
I am new to VBscript and not sure where to go with this one.  Please could someone advice me or help me with code.  I have been looking on the net but not coming up with anything.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
The snippet below should do it, but it will forcibly remove a connections whether it's in use or not. If you don't want it to do that, remove the ", True" part from line 4, but you'll have to put in an error handler to deal with drives which are still in use.
Set WN = WScript.CreateObject("WScript.Network")
Set AD = WN.EnumNetworkDrives()
For i = 0 To AD.Count - 1 Step 2	
   WN.RemoveNetworkDrive AD.Item(i), True
WScript.Echo "All 

Open in new window

Sorry, I didn't finish of the echo line. It should have been:

WScript.Echo "All Mapped Network Drives have been Disconnected!"

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial