batch file to disable network share on Windows XP

5,060

Solution 1

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

Breakdown:

  • 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.)

Breakdown:

  • 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.
PAUSE
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.
PAUSE
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:

/user:[domain\username]

OR

/user:[username@domain]

EXAMPLES:

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

OR

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

OR

net use \\myserver\logs /user:[email protected] /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.

Solution 2

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
NET USE * /DELETE /Y
REM Map connections to the X, Y, Z letters for the shares
NET USE X: \\SERVER1\SHARE1
NET USE Y: \\SERVER1\SHARE2
NET USE Z: \\SERVER2\SHARE1
Share:
5,060

Related videos on Youtube

SuperM4n
Author by

SuperM4n

Updated on September 18, 2022

Comments

  • SuperM4n
    SuperM4n over 1 year

    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
    • SuperM4n
      SuperM4n almost 13 years
      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.
  • hicklypups
    hicklypups almost 13 years
    +1 Beat me to it...Funny, it was there for an hour, then we both must have noticed it.
  • SuperM4n
    SuperM4n almost 13 years
    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?
  • Iszi
    Iszi almost 13 years
    @Robb - If you just need to reconnect the shares, and not actually map them to letters, you can skip the drive letters.