Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How do I write a command line in a .bat or .cmd that maps a network drive? I want the script to first check if the drive-letter is mapped, and if it is delete it and then map the drive.

I only have the mapping-command right now. Please help me fill in the blanks:

REM Check if drive exists, if it does, delete it

@echo off

net use q: /persistent:yes \\localhost\C$\MyFolder


Are there any of the parameters wrong? Any that should be added?

share|improve this question
up vote 11 down vote accepted

You can test for the existence of a drive or folder by testing if the special file "nul" exists in it, i.e.

REM Test if drive exists


GOTO Continue




NET USE Q: /persistent:yes \\localhost\C$\MyFolder

Of course, since you are going to delete it anyway, you could simply delete it and not bother checking for existence first.

share|improve this answer
Great answer thanks... could you just adjust the script to match my example's directory and I'll accept. Thanks! – Seb Nilsson May 28 '09 at 9:33
+1 for the "nul" – alexandrul May 28 '09 at 9:35

You could use this command to delete the mapping (no check required):

net use q: /d
share|improve this answer

Don't bother checking for it, just use "net use q: /delete", which deletes it if it exists otherwise it just return an error.

If you then run the script silently using a bit of vb, the error message won't be displayed (nor will the dos window).

share|improve this answer

Delete drive X: with error messages suppressed:

net use X: /DELETE 2> nul

Delete drive X: with both success and error messages suppressed:

net use X: /DELETE > nul 2>&1
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.