47

Is there any way to connect to a network share via cmd.exe?

3
  • Can you provide more detail? What exactly do you want to do? Copy files, run a program from the network, etc.?
    – aphoria
    Commented Oct 7, 2009 at 12:57
  • @aphoria I just wanted to connect up a drive and use it. Now I'm just trying to figure out the best way to do this since I need to know which drive I'll be using, but I can't guarantee that the drive that I'm using will be Z:
    – leeand00
    Commented Oct 7, 2009 at 16:20
  • @aphoria One thing I definitely like about Linux/Samba better is that you can just select a mount point in your file system and be relatively certain that it's unique.
    – leeand00
    Commented Oct 7, 2009 at 16:22

3 Answers 3

71

use net use, example:

net use X: \\SERVER\Share

Where X: is the drive letter you wish to map the share to, and \\SERVER\Share is the UNC path to the share. This should make the share visible in My Computer and the command line as well like all other shares mapped through the GUI.

In order to later disconnect the share, you would use

net use X: /delete
5
  • 2
    if you want to keep this mapping you can add the switch /persistent:yes Also, sometimes when you try to assign to something that is already assigned you may have to first /delete the mapping. This is the case for printers mapped to LPT ports at least, and if you can't seem to get it working, try that.
    – datatoo
    Commented Nov 22, 2009 at 1:44
  • If you are going across domains, on the same network, you have to use the Server FQDN also yes?
    – user001
    Commented Mar 30, 2018 at 8:33
  • I am trying create a script. Does anyone know how to add username and password as args?
    – Tim
    Commented Sep 18, 2018 at 19:04
  • 1
    If anyone is trying to create a script. You can pass username and password as args. net use X: \\SERVER\Share password /user:DriveDomain\username Docs can be found here: https://www.lifewire.com/net-use-command-2618096
    – Tim
    Commented Sep 18, 2018 at 19:21
  • you can just access the net share without assigning the disk number. like copy \\123.456.78.90\Private\dir1\file1.txt .
    – Chan Kim
    Commented Dec 17, 2021 at 1:40
45

If you don't to map a network drive with net use you can access a UNC Path directly from the Command Prompt using pushd.

For example:

pushd \\server\share

This will create a temporary mapped drive automatically for you and make it your current working directory.

When you're finished on the network share enter the popd command. This will return you to the directory you were in before and delete the temporary network drive.

The popd and pushd commands can be used with local directories. They build up a stack of visited directories which can be handy if you work on the command line a lot. So when you change to a directory with pushd, you can get back to where you were with popd. A stack of directories is built up with each pushd and you go one directory back up the stack with popd.

2
  • 5
    PUSHD \\SERVER\SHARE will silently map a drive. It starts at Z: and moves backwards until it finds an available letter.
    – aphoria
    Commented Oct 7, 2009 at 12:54
  • 1
    @aphoria - I meant to say this in the answer but said "temporary network share" instead of "temporary mapped drive". Have fixed this now. Thanks for pointing this out.
    – David Webb
    Commented Oct 7, 2009 at 14:14
2

If you only want to perform a few actions on the drive (such as move, copy, etc.) then you can use the command line syntax and \ \SERVER\FOLDER\FILE

Example:

'copy \ \Server-01\Folder-01\MyFile.pdf'

That command will copy whatever file you specify into the CWD (Current Working Directory).

Likewise:

md \ \Server-01\NewFolder

To make a directory

rd \ \ Server-01\DeleteFolder

To delete a directory

This method is really only useful if you want to perform one or two operations across the network and can't be bothered mapping the network drive. Or, as in my case, where the network drive is in a different state and the lag when working with files and folders is painful. It's easier to use command line to copy the file to my desktop and view it from there, than try and open it across the network.

Otherwise, I'd use pushd and popd as suggested by Dave Webb.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .