remove network drives in a vb script

Posted on 2008-11-17
Last Modified: 2012-05-05
I have a VB script that connects my network drives without a problem. I added a section so i can remove certain drive letters and where they map to. I need to do this so i can remap the drives somewhere else the section i added to do the remove is below but it is not working.

Dim sUserID
Dim oGroup
Dim oNet
Dim colDrives
Dim sDomainName
Const E_ADS_PROPERTY_NOT_FOUND  = &h8000500D

Set oNet = CreateObject("Wscript.Network")
sUserID = ""
On Error Resume Next
Do While sUserID = ""
  sUserID = oNet.UserName
On Error Goto 0
'wscript.echo sUserid


Sub DriveMap()

'Remove mapped drives

On Error Resume Next
'oNet.RemoveNetworkDrive "E:"
'oNet.RemoveNetworkDrive "F:"
'oNet.RemoveNetworkDrive "G:"
'oNet.RemoveNetworkDrive "H:"
'oNet.RemoveNetworkDrive "I:"
'oNet.RemoveNetworkDrive "J:"
'oNet.RemoveNetworkDrive "K:"
'oNet.RemoveNetworkDrive "M:"
'oNet.RemoveNetworkDrive "N:"
'oNet.RemoveNetworkDrive "O:"
'oNet.RemoveNetworkDrive "P:"
'oNet.RemoveNetworkDrive "Q:"
'oNet.RemoveNetworkDrive "R:"
oNet.RemoveNetworkDrive "u:"

Question by:AllosIT
    LVL 38

    Expert Comment

    Do you have an End Sub at the end of that?  What do get if you comment out the On Error Resume Next?

    It might be simplest to use the script from this article to remove all network drives, and then map the ones you want.

    Author Comment

    Yes i do have an End Sub. If I comment out the On Error Resume Next, I get the following VB script error:

    Line: 39
    Char: 1
    Error: This network connection does not exist
    Which is this line oNet.RemoveNetworkDrive "u:"
    Here is a funny thing if i open the u drive before i run the script the it does not error and just disconnects the drive mapping but does not remove it.

    LVL 38

    Accepted Solution

    Try changing that line to
    oNet.RemoveNetworkDrive "u:", True, True

    This forces removal and also removes it from the user's profile.  See here for the full syntax:

    Author Closing Comment

    Thanks for all your help

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Suggested Solutions

    Are you looking to clear some space on your phone for the latest iOS 8 update? Did you switch to Spotify so you no longer need to keep music native on your phone? Run out of space for taking photos while in the middle of vacation? Sometimes the quic…
    If your app took Google’s lash recently, here are the 5 most likely reasons.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    728 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

    15 Experts available now in Live!

    Get 1:1 Help Now