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.
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:email@example.com /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.