Browse an UNC path using Windows CMD without mapping it to a network drive

163

70

C:\> cd \\somewhere
'\\somewhere'
CMD does not support UNC paths as current directories.

What I usually do to get around this is to map that directory to a network drive and then I could easily access it from the command prompt.

But is there an easier way on how to get around this?

Andreas Grech

Posted 2011-05-13T11:15:40.247

Reputation: 3 522

Use powershell and it works – Kolob Canyon – 2017-01-03T18:38:37.487

Related: How do you handle “CMD does not support UNC paths as current directories”? from Server Fault

– Cristian Ciupitu – 2018-02-14T21:47:26.367

@wullxz That deserves to be an answer. Look at the answers below; "ContextConsole Shell Extension" and "Git Bash" get several upvotes. I think PowerShell is better than third-party solutions. – Franklin Yu – 2018-09-10T20:59:50.150

@FranklinYu added the answer – wullxz – 2018-09-14T14:33:24.150

12which windows do you use? If it's Windows 7, you've powershell preinstalled on your system. Powershell supports unc-paths and cmd-commands (with few exceptions). – wullxz – 2011-05-13T11:27:50.300

You can always browse to the unc path through run. – Supercereal – 2011-05-13T11:45:06.700

1@wullxz: cheers, I didn't know you could browse UNC paths with PowerShell.

@kyle: That's actually a valid answer according to my question :) But I want to access it from a command line environment. Will modify my question to make this more clear. – Andreas Grech – 2011-05-13T12:09:50.140

If your problem is that you need to work just from the command line, you can map the network drive with the 'net' command: net use x: \computer name\share name – Al Crowley – 2011-05-13T12:11:04.070

1@Aleister Crowley: Yes I know, but I'm asking if there's an easier way that doesn't involve mapping a network drive. – Andreas Grech – 2011-05-13T12:12:05.427

@Andreas Grech: I thought that was likely the case, that's why I put it as a comment rather than an answer. – Al Crowley – 2011-05-13T12:13:27.363

Answers

224

If you use pushd and popd instead of cd you won't get that UNC error.

pushd <UNC path> will create a temporary virtual drive and get into it.
popd will delete the temporary drive and get you back to the path you were when you entered pushd.

Example:

C:\a\local\path> pushd \\network_host\a\network\path

U:\a\network\path> REM a temporary U: virtual drive has been created

U:\a\network\path> popd

C:\a\local\path> REM the U: drive has been deleted

C:\a\local\path>

evanmcdonnal

Posted 2011-05-13T11:15:40.247

Reputation: 2 408

2This didn't work for me on Windows 10. C:\WINDOWS\system32>pushd \\some\network\path

' ' CMD does not support UNC paths as current directories. – kayleeFrye_onDeck – 2017-04-04T02:43:20.023

1It's just worked for me in Windows 10, how odd. I did pushd \\ServerName\home\dir\dir2. – Diziet – 2017-05-20T14:36:58.523

3Native and elegant, just a wonder. Works for Windows 10. – WesternGun – 2017-06-22T07:55:51.323

Is there any trick to make it work when the share credentials don't match the machine's user credentials? – Perkins – 2018-03-07T00:51:47.723

works perfect. and I need check how to put these kind of directory into perl script. – Shicheng Guo – 2018-08-10T02:11:54.520

@Perkins: I was able to use runas.exe to run PowerShell as the other user – Jacob Krall – 2018-10-18T18:35:50.347

@JacobKrall That would work if another user has the proper credentials. If it's a totally different set of credentials, use cmdkey. – Perkins – 2018-10-22T20:54:07.620

Works perfect. I was using the sshfs, if it will automatically create a virtual drive (Z, in my case). And access normally to Z like the ones did in C or D. – Kimmi – 2019-05-22T17:37:00.710

13Using pushd creates a drive mapping to the network share and then changes into a path relative to the share it creates. popd disconnects the share. – Dov – 2012-09-06T15:55:27.167

16

I use Git Bash to do this, since I already have it installed.

enter image description here

As an added bonus:
It also has better colors, let's me use ls, rm, etc., and uses the correct slash for paths. :P

Dan

Posted 2011-05-13T11:15:40.247

Reputation: 612

You can also use backslashes if you escape them with backslashes: cd \\\\server\\share\\subdirectory – user121391 – 2016-07-27T08:56:17.797

1Also, if you try to use tab completion with computer names, the shell might lock up for some time until the names are resolved. – user121391 – 2016-07-27T09:03:19.930

it does not work for me – KansaiRobot – 2018-11-06T09:46:54.573

12

Kliu's "ContextConsole Shell Extension" (aka Open Command Prompt) says it, "can even open directories from network paths (UNC paths)" (from an Explorer window).

http://code.kliu.org/cmdopen/

enter image description here

therube

Posted 2011-05-13T11:15:40.247

Reputation: 1 296

remember to open the //drive and not a mapped version. It will fail on mapped. – twobob – 2017-05-04T14:04:50.610

3This program is most awesome! – afrazier – 2011-05-13T16:37:40.483

1i'm shocked by the screenshot, it exist! – LiuYan 刘研 – 2012-06-21T11:01:53.417

8

I also hit the UNC problem with C:\> cd \\somewhere in a C program. Found this page and learnt about the net command: net use x: \\computer name\share name and used it successfully! Thanks to all who post their experiences for others to learn from. :-)

Peter Evans

Posted 2011-05-13T11:15:40.247

Reputation: 81

2@AndreasGrech, Actually pushd does that too. – Pacerier – 2015-01-24T06:48:09.197

This works well in Far+ConEmu, unlike pushd. – Dzmitry Lahoda – 2016-06-03T11:29:50.253

@Pacerier But pushd automatically selects a drive letter and disconnects the network drive when it’s no longer needed (after popd or exit). – Martin – 2019-04-12T15:26:08.997

1Yes, but using net use will map that location to a network drive and that is not what I wanted. – Andreas Grech – 2013-01-28T12:36:14.767

4

If you're using XP you can have a look at this site http://support.microsoft.com/kb/156276

There is a registry value that you need to add, log out, log in again ... and now your cmd.exe does support UNC-Paths. It seems to me that you still can't cd to the path, but you can use it in other commands like dir, copy ...

An alternative might be using the pushd command, that will let you switch to the share (i guess by assigning it a temporary drive letter) http://support.microsoft.com/kb/317379

BigBlackDog

Posted 2011-05-13T11:15:40.247

Reputation: 161

The pages you linked seem like empty pages...... – Pacerier – 2015-01-24T06:49:34.993

The links work here. Have you tried using https? – BigBlackDog – 2015-01-27T08:21:30.453

-1

imho, the most elegant solution is to use mklink /D to create a symlink for the network path. if you use in a script you'll not have to find out which drive letter has been assigned.

franck LZ

Posted 2011-05-13T11:15:40.247

Reputation: 1

The most elegant way to change directories (a read-only operation) is to modify the filesystem?  And where do you create the link?  \Temp?  You might not have write access to the current directory.  And what do you call the link?  What if there’s already a \Temp\somewhere that isn’t already a link to \\somewhere?  What if it’s a file that’s in use?  Etc…  How is this more elegant than pushd? – G-Man Says 'Reinstate Monica' – 2020-01-17T03:22:28.430

-3

You can use the HttpFileServer application, it' over windows, very light and very easy to configure , it allow you to share a network folder UNC ( \server\share ) with HTTP protocol and the HTTP link can be used in any HTML page

http://www.rejetto.com/hfs/

it's amazing

Salman

Posted 2011-05-13T11:15:40.247

Reputation: 3

I agree that this is an amazing piece of software, but how can it be used to get around the fact that the CMD window app does not handler UNC? – Jay Elston – 2018-01-05T22:02:59.263

9Not very useful to a command line user. – Isaac Rabinovitch – 2012-10-20T05:17:33.127