Map Network Drive using DOS command

Create a batch file named mapdrive.bat and put the following inside it:

1
2
3
4
REM Map network drives
net use j: \\server1\john_music
net use p: \\server2\pdfs
pause

Explanation:

  • Line 1 is a comment.
  • Line 2 (and similarly line 3) maps the network share \\server1\john_music to drive J:.
  • Line 4 pauses the DOS window so it doesn’t close automatically.

A few useful additions.

If you want the mapping to survive a reboot, add the /persistent:yes flag. Without it, the mapping is gone the next time you log in:

1
net use j: \\server1\john_music /persistent:yes

If the share requires credentials, pass them with /user: — the password is prompted for if you omit it (which is the safer habit; embedding it in a batch file leaves it readable to anyone with access):

1
net use j: \\server1\john_music /user:DOMAIN\jdoe

To remove a mapping later:

1
2
net use j: /delete
net use *  /delete /yes        REM remove all mappings without prompting

And to see what’s currently mapped:

1
net use

The PowerShell equivalent. If you’re on a modern Windows and want a more scriptable approach, New-PSDrive covers the same ground:

1
2
New-PSDrive -Name J -PSProvider FileSystem `
  -Root \\server1\john_music -Persist

The -Persist flag is the equivalent of net use … /persistent:yes. Note that New-PSDrive without -Persist creates a mapping that’s only visible to the current PowerShell session, which is sometimes exactly what you want for a script that shouldn’t leave drive letters lying around afterward.

This entry was posted in DOS. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


+ 3 = eleven