• Home
  • About 404TS
  • Contact

404 Tech Support

Where IT Help is Found

  • Articles
    • Code
    • Entertainment
    • Going Green
    • Hardware, Gadgets, and Products
    • Management
    • Network
    • News
    • Operating Systems
    • Security and Privacy
    • Software
    • System Administration
    • Talking Points
    • Tech Solutions
    • Web
    • Webmaster
  • Reviews
  • Media
    • Infographics
    • Videos
  • Tech Events
  • Tools
    • How do I find my IP address?
    • Browser and plugin tests
  • Get a Technical Consultation
You are here: Home / Articles / Code / A script to delete mapped network drives using a certain UNC path

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

2013-02-20 by Jason

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.

Filed Under: Code, System Administration

Trending

  • How to configure Windows 8 to auto login
    In Tech Solutions
  • Malwarebytes Anti-Exploit stems from ZeroVulnerabilityLabs acquisition
    In Security and Privacy
  • An In-Place Upgrade will cure what ails your Windows 7
    In Tech Solutions

Latest Media Posts

Find Out Where To Download SNES ROMs

Find Out Where To Download SNES ROMs

Multifunctional Video Conversion Tools – Wondershare Video Converter

Multifunctional Video Conversion Tools – Wondershare Video Converter

  • Popular
  • Latest
  • Today Week Month All
  • Access to the resource [servershare] has been disallowed Access to the resource [servershare] has been disallowed
  • Read the Event Logs on Windows Server Core Read the Event Logs on Windows Server Core
  • Increase IIS Private Memory Limit to improve WSUS availability Increase IIS Private Memory Limit to improve WSUS availability
  • How to ‘Unblock’ multiple files at a time with PowerShell How to 'Unblock' multiple files at a time with PowerShell
  • Setup your DFS namespace with DNS for compatibility in a mixed environment Setup your DFS namespace with DNS for compatibility in a mixed environment
  • How Virtual Reality Supports Mental Health Therapy How Virtual Reality Supports Mental Health Therapy
  • Key Strategies of Successful Coin Listing on Exchange Key Strategies of Successful Coin Listing on Exchange
  • Keeping Your Mac Healthy: A Comprehensive Guide to Maintenance and Troubleshooting Keeping Your Mac Healthy: A Comprehensive Guide to Maintenance and Troubleshooting
  • Making Distributed Software Development Work: Strategies and Best Practices for Managing Remote Teams Making Distributed Software Development Work: Strategies and Best Practices for Managing Remote Teams
  • customer contactless payment for drink with mobile phon at cafe counter bar,seller coffee shop accept payment by mobile.new normal lifestyle concept The Latest Innovations In Payment Technology
Ajax spinner

Elevator Pitch

404 Tech Support documents solutions to IT problems, shares worthwhile software and websites, and reviews hardware, consumer electronics, and technology-related books.

Subscribe to 404TS articles by email.

Recent Posts

  • How Virtual Reality Supports Mental Health Therapy
  • Key Strategies of Successful Coin Listing on Exchange
  • Keeping Your Mac Healthy: A Comprehensive Guide to Maintenance and Troubleshooting

Search

FTC Disclaimer

404TechSupport is an Amazon.com affiliate; when you click on an Amazon link from 404TS, the site gets a cut of the proceeds from whatever you buy. This site also uses Skimlinks for smart monetization of other affiliate links.
Use of this site requires displaying and viewing ads as they are presented.

Copyright © 2025 · Magazine Pro Theme on Genesis Framework · WordPress · Log in