runas from network drive

Microsoft Windows xp professional w/serv...
July 21, 2010 at 11:48:29
Specs: Windows XP
I thought I could make a simple, one-line script to add the current user to the local admins group:

runas /env /user:administrator "net localgroup administrators /add %USERDOMAIN%\%USERNAME%"

...but it doesn't work when run from a network drive because Administrator doesn't have the drive mapped, so I get a "directory name is invalid" error. Which is annoying, because if I run it from a UNC path, it would self-correct to %WINDIR%.

See More: runas from network drive

Report •

July 21, 2010 at 13:18:00
Then map the drive first. Just use NET USE. Can you tell us what you need and we can help.

Report •

July 21, 2010 at 13:24:06
The drive is already mapped, but as the USER, not as admin. After the runas command, I'm running as admin, who doesn't see the drive.

I guess I didn't explain the situation properly. When the batch file is run from a network drive, that drive is the working directory. After switching to the runas admin environment, the admin user doesn't have the drive mapped, so I get a "directory invalid" error, before it even prompts for the admin password.

But I've fixed it. I dunno why I didn't think of it before, but I just issued a %SYSTEMDRIVE% before the runas to change to C:, which obviously exists as admin.


Report •

July 23, 2010 at 12:00:11
Hi lavascript,

Map the network drive as Administrator, and your problem should be fixed.

RunAs cmd.exe as Administrator.
Command: runas/user:administrator cmd.exe
For this you have to type the password for Administrator.

Use 'Net Use' command to map the network drive, with the opened 'Cmd.exe' as Administrator.

Kind Regards,


Report •

Related Solutions

July 23, 2010 at 12:26:59
Okay, like I said, I've found a solution, so it's no big deal, but since people still don't seem to be understanding, try this:

Open a command prompt, map a drive, say a U: drive, and try this:

U:\>runas /user:administrator cmd

You'll get a directory not found error because your working dir is U:, but administrator can't see the U: drive. I don't need a network drive for this script, but i wanted to be able to copy it to one and run it from there. If i had browsed to the dir where the script is directly, i would have been at a unc path, and cmd would have defaulted me to %WINDIR% and there would have been no problem.

pardon the typos, i'm on a phone.

Report •

July 23, 2010 at 14:44:07
Then you need to establish a new system %PATH%. It looks through this path for where the CMD.EXE is. If there is nothing set in the %PATH% then it will not find the CMD.EXE and give you the error you are getting.

So to fix it just run the following command...

set path=%path%;c:\windows\system32

this way it will find the cmd.exe no matter what working path you are in.

Which is what the %windir% is because if you typed echo %windir% you would get "c:\windows\". Like you said the work around is to put the path in before the command like...

%windir%\system32\runas /user:administrator %windir%\system32\cmd

And it would find the RUNAS and CMD executables and you would no longer get the direcotry not found.

I hope this is what you ment but your posts are little hard to follow.

As for the drive mapping thing I understand the Administrator could not see a drive that was mapped under the users session so I was suggesting you map the drive under the administrators session like...

runas user:administrator "net use u: \\server\directory"

So that it would map the drive for the administrator as well.


We could help you better if you just simply tell us what it is that you are trying to do. From the commands you already showed us it look like you are trying to add a Domain User to the Local Administrator group maybe to give them administrator access to run an install or update only an administrator can do hens your attempts at the runas.

Can you simply tell us what it is you are trying to do and there may be a better approach than the one you are attempting?

Report •

July 24, 2010 at 11:16:17
It's not a path thing. cmd is in the path. Actually, I guess I should technically change that part to be %COMPSPEC% for maximum compatibility.

I don't understand what's so hard about what I'm trying to do.

For the sake of saving on typing, when I say RUNASADMIN, I mean runas /env /user:administrator.

If I'm at a C:\> prompt, and I type RUNASADMIN cmd, I get a C:\> prompt as administrator.

But if I'm at a U:\> prompt and type RUNASADMIN cmd, I would get a U:\> prompt as administrator. But the U: drive doesn't exist as administrator, so instead, I get a "Directory not found" error, and I never see a command prompt. I can't run net use, because there's nowhere to run it from.

So I created a batch file that includes RUNASADMIN, and it works fine, as long as it's run locally. If I copy it to a server and browse to it via a UNC path and double-click, it still works, because even though CMD.exe doesn't recognize UNC paths as a working directory, it defaults back to C:\Windows. But if I map a drive, and browse to the batch file via the mapped drive, THAT's when I get the error. Because the U: drive, my current context, DOES NOT EXIST for administrator. By switching my working directory to C: by adding %SYSTEMDRIVE% && before my command, it doesn't matter where the file was run from. So no errors.

PLEASE tell me you understand what's going on now. Please?

Report •

July 25, 2010 at 07:15:35
Okay, I think I may have figured out where some of the confusion lies. I'm using the /env switch with runas, because I need the %USERNAME% variable to refer to the user who's logged in. Without the /env switch, it would be "administrator," which is not helpful.

But another side effect of the /env switch is it opens any command-line tools in the user's current directory, NOT system32 like it normally would for admin.

And if that current directory doesn't exist, the "directory not found" error pops up. I can open notepad as admin just fine, but not cmd or net or any other command-line tools.

Like I said before, sorry for all the confusion. Hope it's all clear now.

And I'm sorry, ace, for not really answering you. But as for what I'm actually trying to do, in the first line of my first post, I said I wanted to add the current user to the local admins group. And while there may be a better way to do that than what I'm doing, I still wanted a way around the "directory not found" problems for any future situations that may arise.

Thank you for taking the time to read my posts.

Report •

February 16, 2011 at 02:55:12
Hi all!!

After 2 weeks of searching, I found the sollution myself to run a program on a mapped network drive with runas...

I want to run a software on mapped drive X: (\\server\share) as administrator

runas /user:server\administrator "cmd /K commands.bat"

net use x: \\server\share
net use LPT1 \\server\printer
cd x:
@echo It works!

(I'm using this to map network drives, and a printer, and then I start a dos program from X:, that uses two mapped drives, and print to the redirected LPT1 without a problem!

Report •

February 16, 2011 at 18:58:25
lavascript.. In your batch file before you put the runas command just put
cd /d %windir% , it will instantly switch the PWD to normal local Boot drive and you are good to go,

Ace, your expnantion and logic here is perfect but not for oversmart questioners who assume we know whats going on in their mind..

Subhash Chandra.

Report •

Ask Question