Subscribe

Get the Network Administrators tool pack

Subscribe to our newsletter and get 11 free network administrator tools, plus a 30 page user guide so you can get the most out of them.

Click Here to get your free tools

Recent Posts

Search

Archives

Remove unavailable drives from a script

Post image for Remove unavailable drives from a script

by Steve Wiseman on October 21, 2011 · 5 comments

in Tips,Tools,Utility


.

I have been testing parts of our software lately (Alpha stuff that will be released in a few months). One problem I keep running into, is I collect up a large number of mapped drives that are unavailable:

Unavailable Drives

This is because the machine is powered off, or is a virtual machine that I deleted.

I like to use the same set of drive letters for testing, since it makes it simpler, and I don’t have to keep changing paths. The problem is that once a drive becomes ‘unavailable’ you need to to explicitly disconnect each one by using the command:

net use /delete [drive]

I was getting tired of doing this all the time, so I built a script that will automatically clear out disconnected or unavailable drives.

Here is the script:

@echo off
for /F "tokens=1-3" %%a in ('net use') do (
if /i %%a==Disconnected net use /delete %%b
if /i %%a==Unavailable net use /delete %%b
)

It loops through all the network connections, and looks for ‘Disconnected’ or ‘Unavailable’ and runs a ‘net use /delete [drive]’ against it.

I ran it once on my machine:

Unavailable Drives Deleted

It cleared out all of those unavailable drives in a few seconds.

This can be helpful in your login scripts too – if the user has failed mappings from before, it can prevent your logon script from properly mapping to the correct server.

If you want to create a shortcut to it on your desktop, just create a new text file, name it “clear_drives.bat” and put the above script in.

Now if you want to clear out your unavailable drives you just need to double click on a shortcut, instead of wasting time in the command line.

One more thing…Subscribe to my newsletter and get 11 free network administrator tools, plus a 30 page user guide so you can get the most out of them. Click Here to get your free tools

Related Articles:

{ 5 comments… read them below or add one }

1 David October 21, 2011 at 4:39 pm

This is great Steve! Can the same thing be done for printers? I have a client where I also map network printers (via a VB login script). It works fine but whenever there is a printer removed or renamed, they never get removed from the workstation.

2 Steve Wiseman October 21, 2011 at 8:32 pm

Good question. I think this script might actually work – if you map the printer to an LPT port.

Otherwise…when you have issues with those printers, do they show up as disconnected?

3 Angus Scott-Fleming May 8, 2012 at 6:43 pm

Looking for a USB tool that can assign a USB drive a fixed letter based on its “Volume Name” (e.g. “\\?\Volume{add04065-60ff-11df-b514-001617ad622c}\”) or its “Volume Label” (e.g. TREO744P). Are you aware of or do you have such a tool?

USBDLM from Uwe Seiber would work, except it installs a Windows Service, which I really don’t want to do on client machines, especially servers. In addition, because it’s not free for commercial use, I could not buy 1 license and then install it on client machines to make my USB drives have static letter assignments. I would be willing to buy software if I could use it everywhere.

4 Steve Wiseman May 9, 2012 at 10:09 am

Hello Angus Scott-Fleming,

I think it would have to be a service since it would need to sit in the background and watch the USB drive get mounted, and then switch it to the drive you wanted. I will put this on the list of possible free utilities. Not sure how hard it is yet…so I can’t promise…but it would be a nice free tool to have 🙂

5 John August 14, 2012 at 5:28 pm

Not the same thing, but a qucik way to clear ALL mapped drives is “net use /d *”. Works like a charm for me.

Leave a Comment

Category Links - Windows Forum - Exchange Forum