Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Loosely related to this question Network Share causing Cygwin to run slowly after 'ls', I'd like to write a little batch file that I can execute to disconnect the host from any network shares and subsequently another batch file to reconnect. Ideally, this would be something that I can execute from a PuTTY terminal, SSHed into the box running cygwin.

I'm pretty sure the batch files can be written easily, but I don't know about executing them from a PuTTY terminal. Regardless, I'd still like the batchfiles anyways.

For the sake of simplicity my process would be:

  1. Log into server via PuTTY
  2. Run batch files to disconnect shares
  3. Do what I need to do
  4. Run batch files to reconnect shares
  5. Exit session, closing PuTTY
share|improve this question
Using cmd /c <batchfilename> allows a cygwin user to execute a batch file on the system. So when I SSH in, I just run this command to take the network shares down, and then run another to bring them back up if I need to. –  Robb Jun 25 '11 at 19:53
add comment

2 Answers

up vote 2 down vote accepted

Note that the following needs to be run with an account in the local Administrator group, and preferably one that has access to the shares being mapped.

The basic commands

To disconnect drives:

net use /d * /y


  • net use is the utility for changing networked drive mappings from the command line.
  • /d is for "delete", to disconnect whatever drive mapping is specified in the command parameters.
  • * is a wildcard, to run the command on all currently mapped drives.
  • /y is for "yes", to bypass interactive confirmation of the command.

To reconnect drives:

net use [driveletter]: "\\[servername]\[sharename]\[subfolder-path]" /p:y

(Repeat for each mapping.)


  • net use - (See above)
  • [driveletter]: - Replace this with whatever drive letter you want to use. Remove the brackets, keep the colon. You can remove this entirely if you don't need to associate the share with a drive letter.
  • "\\[servername]\ - Replace this with the name or IP of the machine hosting the share. Remove the brackets. Keep the back-slashes. Quotation marks are optional if the path does not include spaces.
  • [sharename] - Replace this with the name of the share you are accessing. Remove the brackets.
  • \[subfolder-path] - (Optional) Replace this with the remaining path to whatever sub-folder you want the mapping to address. Leave it out if you just want the mapping to point to the root of the share. Remove the brackets. Use back-slashes where appropriate.
  • " - Leave the closing quote if you kept the opening quote. Remove otherwise.
  • /p:y - This is for "Persistent:YES", meaning that the drive mapping will be retained through a reboot.

Sample batch file with comments

REM The first line below keeps the batch commands from "echoing" on the command line.  Only command output is displayed.  Delete or comment out that line for debugging.
@echo off
REM The next command deletes all drive mappings.
net use /d * /y
REM The next two lines print a message stating what the previous line should have done (check command output to verify) and what the user should do next.
echo Drive mappings DELETED!
echo Press any key to restore drive mappings.
REM The next line pauses the batch job, pending user input.  Leave the batch window open, and go on to do your work.  Return to the batch window and press any key to continue.
REM The next command is an example of connecting to a share path that does not include subfolders or spaces, and will not be mapped to a drive letter.
net use \\myserver\logs
REM This next command is an example of mapping a drive letter to a path that includes a subfolder, but no spaces.
net use R: \\myserver\myapp\reports /p:y
REM This next command is an example of mapping a drive letter to a path  that does include sub-folders and spaces.  Note the requisite quotation marks.
net use P: "\\myserver\c$\Program Files\My Application\"
REM The next two lines print messages similar to the previous two "echo" commands, this time informing the user that the drives should be re-mapped.
echo Drive mappings RESTORED!
echo Press any key to exit.
REM This last line inserts a final pause in the batch job.  Use this opportunity to check the command output and verify that the previous commands completed succesfully.
REM The batch window should automatically exit after this, or return to the command prompt if the file was run from within an existing console.

Note that this could easily be split into two separate batch files if needed, with the divide placed immediately after the first PAUSE. I strongly recommend keeping the PAUSE commands at the ends of the batch files even if they are split, so that you can confirm the batch commands were successful before it exits.

Mapping as another user

If you cannot run the net use command as a user with access to the shares, an additional parameter and some further user interaction will be required for each mapping.

No additional parameters are required to delete drive mappings.

To re-connect drives as a different user, add the following parameter to each command:





net use R: \\myserver\reports /user:mydomain\me /p:y


net use P: "\\myserver\c$\Program Files\My Application" /user:mysubdomain.mydomain.tld\me /p:y


net use \\myserver\logs /user:me@mysubdomain.mydomain.tld /p:y

For each mapping, you should be prompted to enter your password. I believe there is an additional parameter available that allows you to include your password in the command. However, since batch files are stored in cleartext, I strongly recommend against using it.

share|improve this answer
add comment

Try this - no idea how it will work in Cygwin... but this is BATCH which should work if executed on Windows normally.

REM Delete all existing connections
REM Map connections to the X, Y, Z letters for the shares
share|improve this answer
+1 Beat me to it...Funny, it was there for an hour, then we both must have noticed it. –  KCotreau Jun 24 '11 at 17:13
Stupid question but if I don't specifically map these shares to drives, will this batch still work? What I mean is I've never actually mapped the shares, the first part of the batch will still succeed? –  Robb Jun 24 '11 at 17:24
@Robb - If you just need to reconnect the shares, and not actually map them to letters, you can skip the drive letters. –  Iszi Jun 24 '11 at 17:45
add comment

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.