Home » Code »System Administration » Currently Reading:

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

February 20, 2013 Code, System Administration 3 Comments

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 \\server\share /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 \\servername\sharename
mkdir c:\drives
wmic netuse where "RemotePath like '%%servername%%'" get localname > c:\drives\baddrives.txt
more c:\drives\baddrives.txt +1 > c:\drives\baddrives2.txt
REM loop through file of drive letters to remove
for /F %%A in (c:\drives\baddrives2.txt) do net use %%A /DELETE
DEL /F /Q c:\drives\baddrives.txt
DEL /F /Q c:\drives\baddrives2.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.

 A script to delete mapped network drives using a certain UNC path
Jason Hamilton is an IT Professional in his full-time job and operates 404 Tech Support LLC in his spare time from Central Illinois. Send us a story suggestion or inquire about advertising on 404TechSupport.com with the Contact Page.

  • bob

    always loving a good challenge!

    just use 1 line:

    for /f “tokens=1 delims=:” %%i in (‘net use ^| find /I “servername”‘) do net use %%i: /delete

    replace servername with your actual servername and you’ll remove every mapped drive to that server. As a handy alternative you can use the share name…or full unc path.

    put it in a trusty .cmd file and you’re ready to go

    • JasonH09

      Great! I tried to simplify things using the for loop for more like skipping the first line of the output text file but it just seemed to break the script on me.
      Thanks for the revision!

      • JasonH09

        Looks like I jumped the gun in promoting this. It gives an error running on Windows 7 Enterprise.

        This worked well though:

        for /f “tokens=2 delims= ” %j in (‘net use ^| find /I “servername”‘) do (for /f “tokens=1 delims=:” %i in (‘echo %j’) do net use %i: /delete)

        (Replace servername with the name of the server.) Credit: My boss, who took on the challenge

Subscribe

RSS feed Subscribe by email Find us on Facebook Follow us on Twitter Circle us on Google+   Donate to 404TechSupport via PayPal.

Search 404 Tech Support articles

Custom Search

Sponsors

Do It Smarter Managed IT Services
Do-It-Smarter Managed IT Services - Managed backup, hosted email, systems management, network audits, managed security & disaster recovery.

Buy Electronics
Buy Electronics: Solar Charger, Mini Speakers, TV Box, MP3 Player, Power Inverter at Wholesale Price on DHgate.com

Drop Shipping
Specializing in Drop Shipping at Tmart

404 Tech Support Network