We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Removing a mapped network drive

Medium Priority
492 Views
Last Modified: 2012-05-06
I think I am loosing it...I going to narrow it down to a specific 3 line piece of code, using a vbscript I want to remove a previously mapped drive.

Dim WshNetwork
Set WshNetwork = Wscript.CreateObject("WScript.Network")
WShNetwork.RemoveNetworkDrive "M:"

After running this code and I look at "My Computer" the drive still shows with a red X.  Double click on the drive opens it and removes the red X.  Attempting to map a new location to the "M:" drive produces an error saying already mapped...am I missing something sim;e?
Comment
Watch Question

Commented:
how about a simple:

net use m: /del

this is just batch

Commented:
or try this

Option Explicit
Dim objShell, objNetwork, DriveLetter1

DriveLetter1 = "M:"

Set objShell = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("WScript.Network")

objNetwork.RemoveNetworkDrive DriveLetter1
objShell.PopUp "Drive " & DriveLetter1 & " disconnected."

Wscript.Quit

Author

Commented:
Cannot, do not, want to use a batch file.  The logon script (where this code is used) is larger than just mapping drives, it also controls the printers and the default printers.  The code you recommended is almost identical to mine with the exception of assigning the drive letter to a variable and the message at the end..  Still, I did copy your suggestion verbatim into a new test script and ran it.  It came back with "M: disconnected" and showed in My Computer with a red X.  Double clicking on the M Drive still opened the M drive and showed me the files.  I ran the script again, it came back again with "M: Disconnected".  I then ran the script again, it came back with "The network connection does not exist", although double clicking the M drive in My Computer still opens it.

Author

Commented:
Found a solution that worked....changed the below line to read

WShNetwork.RemoveNetworkDrive "M:", true, true

This does a more through removal of the drive mapping.
DonNetwork Administrator
CERTIFIED EXPERT

Commented:
This will remove all mapped drives...

'****Remove all mapped drives****
For intDrive = 0 To CheckDrive.Count - 1 Step 2
    wshNetwork.RemoveNetworkDrive CheckDrive.Item(intDrive), bforce
Next
DonNetwork Administrator
CERTIFIED EXPERT

Commented:
should be  >>>>

Set objNetwork = CreateObject("Wscript.Network")
Set colDrives = objNetwork.EnumNetworkDrives
For i = 0 to colDrives.Count-1 Step 2
    objNetwork.RemoveNetworkDrive colDrives.Item(i)
Next

Author

Commented:
I have been using VB Scripting for a long time now to map the drives and printers for new users and to remove and change printers as printers change for existing users.    We just finished setting up a DFS and needed to change the mapping of the drives.  This must be the first time that I have had to change/delete the mapping of drives and
WShNetwork.RemoveNetworkDrive "M:"
DOES NOT WORK, it only disconnects the drive, but leaves it mapped
WShNetwork.RemoveNetworkDrive "M:", true, true
DOES WORK
By the way, I do use a similar type looping script to first verify if a drive needs to be mapped before deleting it and re-adding it.  I found users got confused if they didnt wait for the logon script to finish running before they started opening up drives and found them not available.

DriveM = 0 
DriveS = 0
if AllDrives.Count > 0 then 
  a = 0
  do
    If AllDrives.Item(a) = "M:" then 
      a = a + 1
      If ucase(AllDrives.Item(a)) = "\\Domain\PUBLIC" then 
        DriveM = 1
      end if
    elseIf AllDrives.Item(a) = "S:" then 
      a = a + 1
      If UCase(AllDrives.Item(a)) = "\\Server\CLIENTAPPS" then 
        DriveS = 1
      end if
    else
      a = a + 1
    end if
  loop until a >= AllDrives.Count
End if
 
If DriveM = 0 then 
  On Error Resume Next
  WShNetwork.RemoveNetworkDrive "M:", true, true
  WshNetwork.MapNetworkDrive "M:", "\\Domain\Public", True
End if
 
If DriveS = 0 then 
  On Error Resume Next
  WShNetwork.RemoveNetworkDrive "S:", true, true
  WshNetwork.MapNetworkDrive "S:", "\\Server\ClientApps", True
End if

Open in new window

Commented:
thanks for the post back, shouldve written the script up in something other than notepad and i would have seen the optional arguments

Author

Commented:
I would be interested in a better program than notepad for script editing....trying to find the error on line 74 is a pain in notepad...
Network Administrator
CERTIFIED EXPERT
Commented:

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.