Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 994
  • Last Modified:

remove network drives in a vb script

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:"

  • 2
  • 2
1 Solution
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.
AllosITAuthor Commented:
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.

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:
AllosITAuthor Commented:
Thanks for all your help

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now