How Can I Find All the Drives Mapped to a Share and Remap Them?

How Can I Find All the Drives Mapped to a Share and Remap Them?

  • Comments 3
  • Likes
Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I find all the drives mapped to \\server1\share and remap them to \\server2\share?

-- H T-S

SpacerHey, Scripting Guy! AnswerScript Center

Hey, H T-S. You know, not too long ago Malcolm Gladwell published a book called The Tipping Point. Loosely speaking, the book posited that something could be ignored for the longest time, at least until it reached the so-called tipping point; upon doing so, this thing that nobody cared about would suddenly became a veritable craze. Seemingly overnight it would go from being something you never heard of to something that seems to show up everywhere you look.

It’s an interesting conjecture, and we seem to be seeing this phenomenon when it comes to scripts that map and unmap network drives. For over a year we published the Hey, Scripting Guy! column without mentioning network drives, and nobody seemed to notice. And then, suddenly, we were deluged with questions about mapping and unmapping network drives. We answered the first such question a couple weeks ago; now we’re back to answer another one, and we have an Inbox full of additional questions about network drives. First hula hoops, then bell-bottom pants, now network drives. Go figure.

So what about remapping network drives? Well, for better or worse there’s no RemapNetworkDrive method that can take care of thus automatically; therefore, we have to come up with a minor workaround. But that’s not too bad: we can carry out this task by locating any drives that meet the criteria, unmapping those drives, then remapping each drive to the new location.

Sure, it sounds complicated, but it’s really pretty easy. Here’s a script that looks for any drive mapped to \\server1\share and remaps it to \\server2\share:

Set objNetwork = CreateObject("Wscript.Network")

Set colDrives = objNetwork.EnumNetworkDrives

For i = 0 to colDrives.Count-1 Step 2
    If colDrives.Item(i + 1) = "\\server1\share" Then
        strDriveLetter = colDrives.Item(i)
        objNetwork.RemoveNetworkDrive strDriveLetter
        objNetwork.MapNetworkDrive strDriveLetter, "\\server2\share"
    End If
Next

The script starts off by creating an instance of the Wscript.Network object. We should note that we need to use Windows Script Host any time we want to map or unmap network drives; that’s because WMI doesn’t have any methods for mapping or unmapping drives. That’s fine, except it means that our script must be run on the local computer: WSH methods typically can’t be used against remote machines. That’s a limitation you’ll just have to deal with. (One way to deal with it: run the script as a logon script. Logon scripts always run locally.)

After creating the Network object we then call the EnumNetworkDrives method in order to return a collection of all the mapped network drives on the computer:

Set colDrives = objNetwork.EnumNetworkDrives

That brings us face-to-face with that curious little animal known as the mapped network drives collection. We won’t detail the architecture of this collection today; for that, see our previous column on network drives. Suffice to say that each mapped drive actually takes up two items in the collection: the first item for the drive letter, the second for the UNC path. If we have three mapped drives on a computer our collection will look something like this:

X:
\\server1\share1
Y:
\\server2\share2
Z:
\\server3\share3

That’s why we have to use such a crazy-looking For Next loop in order to walk through the collection; this line of code causes us to skip every other item in the collection, in turn ensuring that we land only on the drive letters:

For i = 0 to colDrives.Count-1 Step 2

For each drive letter we then need to determine whether the corresponding UNC path is equal to \\server1\share1. Remember, if we’re looking at item 0 in the collection (the first item in the collection has an index number of 0) then we’re looking at a drive letter; the corresponding UNC path will be that index number (0) plus 1. Therefore, we use code like this to determine whether or not the first drive happens to be mapped to \\server1\share1:

If colDrives.Item(i + 1) = "\\server1\share" Then

Let’s assume that it is. In that case, we need to grab the drive letter (item 0) and store that value in a variable named strDriveLetter. We then call the RemoveNetworkDrive method to unmap the drive, then call the MapNetworkDrive method to remap that same drive letter to the new share:

objNetwork.MapNetworkDrive strDriveLetter, "\\server2\share"

And no, they don’t call it the “tipping point” because you just tipped over trying to follow all this. We know it’s a bit confusing, but that’s because of the strange way in which the mapped network drives collection is constructed. If this doesn’t make any sense to you, try drawing it out in a flowchart of some kind; you should see that there is a logic to the whole thing. A somewhat twisted logic, perhaps, but a logic nonetheless.

Because this is confusing we went with the simplest possible example: a share named \\server1\share is remapped to a share named \\server2\share. It’s also possible to remap any share on server1 to any similarly-named share on server2. We thought that was a bit much for today, however. If that’s of interest to you, though, just let us know, and we’ll revisit the topic in the near future.

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Any chance you can post how to remap any share on server1 to any similarly named share on server2? This is exactly what I need to do. :)

    Thanks!

  • You can use this code to remap a share from one server to a share of the same name on another server.

    On Error Resume Next

    strOldServer = "server1"

    strNewServer = "server2"

    Set objNetwork = CreateObject("Wscript.Network")

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    Set colDrives = objNetwork.EnumNetworkDrives

    For i = 0 to colDrives.Count-1 Step 2

    strThisServer = Left(Mid(colDrives.Item(i + 1), 3), InStr(Mid(colDrives.Item(i + 1), 3), "\") - 1)

    If LCase(strThisServer) = LCase(strOldServer) Then

    strDriveLetter = colDrives.Item(i)

    strNewPath = "\\" & strNewServer & "\" & Mid(colDrives.Item(i + 1), Len("\\" & strOldServer & "\") + 1)

    If objFSO.FolderExists(strNewPath) = True Then

    objNetwork.RemoveNetworkDrive strDriveLetter

    objNetwork.MapNetworkDrive strDriveLetter, strNewPath

    End If

    End If

    Next

  • This doesn't seem to be working in PowerShell as of 2012.