Subscribe

Get the Network Administrators tool pack

Subscribe to our newsletter and get 11 free network administrator tools, plus a 30 page user guide so you can get the most out of them.

Click Here to get your free tools

Recent Posts

Search

Archives

Simplify UNC usage in command line batch files

by Steve Wiseman on February 19, 2007 · 21 comments

in Windows


.

Many times a UNC path does not work properly in a command line bat file. This is a hold over from DOS, a compromise to ensure backward compatibility.

Command Line UNC

I have found a little known command (At least to me). Actually there are two of them.

PUSHD, and POPD

Call PUSHD with a UNC share as a single argument and it automatically maps the UNC share to a drive letter. (It starts at Z: and moves backward). So, if we wanted to run a few command line programs within our share we would call it like this:

pushd \\server\share
command1.exe
command2.exe

Command Line UNC PUSHD

When you are finished, and want to remove the mapping you call popd. Putting it all together we have this:

pushd \\server\share
command1.exe
command2.exe
popd

Command Line UNC POPD

I have tested this with Windows NT 4 SP6, and it works all the way through Vista…so you are safe using it on any of these versions of Windows.

One more thing…Subscribe to my newsletter and get 11 free network administrator tools, plus a 30 page user guide so you can get the most out of them. Click Here to get your free tools

Related Articles:

{ 21 comments… read them below or add one }

1 Nihal March 1, 2010 at 3:53 am

Thanks! This worked perfectly when using batch files in XP Mode from within Windows 7 (I had to use it to run Fortran, long story 🙂 )

2 Ciprian Cornea June 22, 2010 at 11:15 am

Thanks Steve!

It did the job also for me too.
Now, the Windows Task Scheduler is able to run my batch job to copy files from a network drive to a Sharepoint share.

Ciprian.

3 Zim June 29, 2010 at 10:25 pm

Won’t map a drive for UNC paths deeper than 2 folders.
Why?

4 ashit kamani July 1, 2010 at 11:14 am

Hii, thanx a lot
dis worked perfectly for me in my project.

5 Cooper September 10, 2010 at 5:51 am

THX a lot … you’ve saved my life 🙂
this works fine for me 🙂

6 Padraig January 21, 2011 at 1:09 pm

no this did not work
you click on the batch file script6.bat
which is in \\defile1\apospay
‘\\defile1\apospay’
CMD.EXE was started as the current directory
UNC paths are not supported. Defaulting to Windows Directory
SIMPLEST BATCH FILE –> Follows
pushd \\defile1\apospay
“C:\Program Files\Ipswitch\WS_FTP Professional\ftpscrpt.com” -f \\defile1\apospay\script5.scp
popd

7 Gilad May 2, 2011 at 9:17 am

GREAT ONE!!!!!!!!!!!!

8 Citizen May 25, 2011 at 12:02 pm

This isn’t a hold over from DOS, it’s like by design to avoid potential problems from launching processes from a UNC path and closing the console with the process still running.

It’s also something that can be turned off:
http://support.microsoft.com/kb/156276

9 Steve Wiseman May 25, 2011 at 12:32 pm

Awesome information. Thanks!

10 samba September 23, 2011 at 3:58 pm

This command worked like a charm to me while working with UNC share. Thanks for sharing!!

11 Cole October 28, 2011 at 12:21 pm

Nice! Thanks.

12 anil November 8, 2011 at 4:49 am

Thanks a lot

13 Sander March 20, 2012 at 5:03 am

Great, thanks!

14 Dhanush April 4, 2012 at 9:47 am

It works! Great!

15 Habeeb May 4, 2012 at 5:12 am

WELL…. but if it s asking login credentials , how can i give it…?

16 Freek May 7, 2012 at 11:45 am

@Habeeb, try cmdkey!

17 chetan November 9, 2012 at 9:34 am

Hi

I want to execute inventory software (network share).I want to execute this client with domain admin rights i.e slient installtion with out any interaction with users.
How can I use u/n and p/w with this for slient installtion

18 Sergiu Z March 5, 2013 at 6:09 pm

THANK YOU!

None of the other sites i visited that talked about the net use command mentioned the pushd and popd which are hugely useful.

19 Vicki Brown April 29, 2013 at 3:10 pm

I was looking high and low for a net use command and this is the only site where I found the pushd and popd.

Thank you!

Vicki Brown

20 Joe Kandan November 11, 2013 at 9:01 pm

Unfortunately for me, I am trying to access a shared db from a ftp server. I can access the db by supplying the credentials of the ftp server. By using pushd and popd, it gives the error saying the “the network path not found” Is there a way around this?

21 Rhonda Frazier March 14, 2014 at 7:28 pm

Thank you for the pushd and popd! All the other developer websites had examples with net use…something the Sr. IT person did not want to use.

cheers!

Leave a Comment

Category Links - Windows Forum - Exchange Forum