Script to remove a mapped network drive and then add it back.

Discussion in 'Scripting' started by Ali, Dec 20, 2003.

  1. Ali

    Ali Guest


    This script can be run against users, deletes the mapped
    network drive U:, then adds a mapped drive right after.

    Set WshShell = WScript.CreateObject("WScript.Shell")
    Set WshNetwork = WScript.CreateObject("WScript.Network")
    Set AllDrives = WshNetwork.EnumNetworkDrives()

    DriveLetter = "U:" 'must be capitalized
    RemotePath = "\\server\share"

    AlreadyConnected1 = False
    For i = 0 To AllDrives.Count - 1 Step 2
    If AllDrives.Item(i) = DriveLetter
    Then AlreadyConnected = True

    If AlreadyConnected1 = False then
    WShNetwork.MapNetworkDrive DriveLetter, RemotePath

    WShNetwork.RemoveNetworkDrive DriveLetter
    WShNetwork.MapNetworkDrive DriveLetter, RemotePath
    End if
    Ali, Dec 20, 2003
    1. Advertisements

  2. No need to enumerate all mapped drives just to find out whether or not "U:"
    is already mapped:

    set FSO = createobject( "scripting.filesystemobject" )
    if ( FSO.DriveExists( "u:" ) ) then
    WshNetwork.RemoveNetworkDrive "U:", true, true
    end if
    WshNetwork.RemoveNetworkDrive "U:", true, true

    Also, no need to worry about capitalizing the drive letter names in this

    Another way to do this would be to use removenetworkdrive in all cases and
    ignore the error.

    Al Dunbar [MS-MVP], Dec 26, 2003
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.