404 Tech Support

A script to delete mapped network drives using a certain UNC path

Mapped drives. You can love ’em or hate ’em.

After changing file servers or retiring an old server, clients might have old mapped drives pointing to these servers. Since they could be a confusing interface and disconnected drives might slow down the computer until they time out, it’s not a bad idea to clean up the old ones. It’s a pretty simple process to disconnect a mapped drive when you are in front of the computer. You just right click on the drive and choose ‘Disconnect’. There are also command line options if you’re at the computer with the user logged in.

Scripting the removal is also easy if you know the drive letter. It can just be a simple:
Net Use Z: /Delete

However, many organizations are in chaos with mapped drive letters and if you ran a script like that to delete all Z drives, you might disconnect some current, useful drives with clients that don’t know the path where it was mapped. To utilize a script, it’s going to have to have some intelligence built into it to ensure you are disconnecting the correct drives. You would run into the same problem with using Group Policy Preferences to Delete a connection by drive letter.

This solution was a little more satisfying than most because I did a lot of research online going down various paths. The first problem was that Microsoft’s built-in tools do not have a smart way of handling this problem. For example, you can disconnect a network share by the UNC path if it hasn’t been assigned a drive letter e.g. Net Use \servershare /delete but once it has been assigned a drive letter, you have to use that for the disconnect script. Using WMI, you can implement some filtering logic with WHERE statements and LIKE comparison operators but WMIC does not have permission to modify or delete network connections for some reason. Using something like WMIC netuse where “RemotePath like ‘\servername’ DELETE results in “ERROR: Description = Provider is not capable of the attempted operation.”

The second reason this solution is rather satisfying that there was no clear, straight-forward solution online. Worst of all, there were plenty of threads on various sites asking to solve this exact problem but others did not understand it. There were various responses asking endless questions or less helpful suggestions like use “net use * /delete and then map the drives you want with Group Policy Preferences”. Thanks but that’s not really a great solution…

Anywho, this is what I came up with. You use a WMIC command to filter your drive letters to those that match the UNC path you are interested in. There is certainly room for improvement or more gracefully handling errors but it fails through silently without getting hung up and completes successfully. Part of that is knowing my environment, so this script is at work in my environment to clean up old drive letters.

@echo off
REM Jason Hamilton
REM www.404TechSupport.com
REM Script unmaps drives with a particular string in the path
REM Set path in WMIC query. Use underscore in place of a slash for a 1 character wild card
REM '%%servername_sharename%%' would match \servernamesharename
mkdir c:drives
wmic netuse where "RemotePath like '%%servername%%'" get localname > c:drivesbaddrives.txt
more c:drivesbaddrives.txt +1 > c:drivesbaddrives2.txt
REM loop through file of drive letters to remove
for /F %%A in (c:drivesbaddrives2.txt) do net use %%A /DELETE
DEL /F /Q c:drivesbaddrives.txt
DEL /F /Q c:drivesbaddrives2.txt
RMDIR /S /Q c:drives
exit

One improvement could be to update the script to accept a parameter which would be the server path string you want to search for and you could enter this from the Group Policy logon scripts interface. For now, you just edit the WMIC command to change the path to what you are looking for. You can use the underscore as a 1 character wild card in place of a slash since there currently isn’t a way to escape a backslash character.

Hope the script is of use to anybody in a similar situation. After you have the old drive letters cleaned up, you can change to a centrally managed means like Group Policy Preferences or at least a logon script without persistent connections. Feel free to share any suggestions for improvements you might make in the comments.