Script to disconnect mapped network location
Solution 1
As long as there are no other shares you are trying to save you can use the following in a batch or logon script:
NET USE * /delete
NET USE O: \\server\share
This will remove all assigned (*
) shares and set it to the drive/path you want.
Solution 2
To identify the drive letter that the share is currently mapped to and unmap it:
@echo off
for /f "tokens=2" %%D in ('net use ^| find ":" ^| find "\\server\share") do net use %%D /delete
Comments:
^
is like\
in Unix –– it turns the following character from special to non-special, or vice-versa. We use^|
so that the interpretation of the|
characters is deferred until the command inside the parentheses is parsed.%%D
is aFOR
loop index variable. You can use any letter you want, but, unlike normal Windows Command Prompt variables,- it can be only a single letter, so don’t try to use
%%DRV
, and - it is case-sensitive.
- it can be only a single letter, so don’t try to use
- I included the
find ":"
because, when I was testing this, I got anet use
output that looked like
OK H: \\WONDERLAND\HUMPTYDUMPTY OK Q: \\WONDERLAND\QUEENOFHEARTS OK \\WONDERLAND\CHESHIRECAT
So you need to do something to filter out the lines that don’t include a drive letter.
- As you may know (because I hinted at it, above),
FOR
is a loop construct. Your statement(s) get executed once for each line that comes out of thenet use ... | find
pipeline, with%%D
set to the second string (token) on the line. If you want to do more than one command, and/or be more selective, you can do this:
for /f "tokens=2" %%D in ('net use ^| find ":" ^| find "\\server\share") do ( ... if %%D==Z: ( ... ) else ( ... ) ... )
Want more? Change "tokens=2"
to "tokens=2,3"
, and %%E
will be set to the third token; i.e., the UNC that the drive letter is mapped to.
Related videos on Youtube
Kruug
I specialize in help desk/desktop support. I dabble in both Windows and Linux, as well as a bit of programming (mainly web-based currently).
Updated on September 18, 2022Comments
-
Kruug over 1 year
I have a user who needs to map a certain network location to drive
O:
. Every so often, this drive loses it's map toO:
, but will show up under different letters, such asH:
,K:
, andL:
. The normal procedure is for him to call Helpdesk (me) to come down and unmap/remap the drive to the proper letter.During the remap process, I make sure to check the box
Remap on login
but that setting doesn't seem to take hold.Is there any way to script this task? Normally, I would use
net use /delete \\server\share
ornet use x: /delete
(where x: is the drive letter), but the first one does not work as there is a letter assigned to the path, and the second one doesn't work as the letter it remaps to isn't always the same one.System is Windows NT (we use specialized software that hasn't had an update in a while).
-
Scott - Слава Україні almost 11 yearsWrite a script (“batch file”) to do
net use
and pipe it throughfind
orfindstr
to identify the drive letter that the share is currently mapped to. -
Kruug almost 11 years@Scott Can you expand on that script? I have very little scripting knowledge within Windows.
-
-
Kruug almost 11 yearsYeah, there are other shares. Do you have one that works with the
find
orfindstr
that Scott mentioned above? -
edwardbackstrom almost 11 yearsIt can be done but not easily with DOS batch scripting. Finding the share is easy but acting upon it is much more difficult and means a more complex and bloated script. Is only one user affected? Can you add the additional shares after the code listed above?
-
edwardbackstrom almost 11 yearsActually, there is no real reason to "over-tech" the problem. As long as the share name is correct, you should be able to just use
NET USE \\server\share /delete
instead ofNET USE * /delete
. Then continue and assign it to the correct drive letter. -
Kruug almost 11 yearsYeah, I tend to have that issue (over-teching) when looking at a problem.