"Run as" for a .bat file

20

6

I'd like to run a .bat file as a different user to troubleshoot a rights problem which I suspect.

For normal .exe files, you can Shift-right-click to get "Run as...". However, for a .bat file, this does not work (Shift-right-click gives no menu entry "Run as..."). Why is that so, and how can I "Run as" a .bat file? Is it possible at all?

System is Windows XP SP2 Home Edition.

sleske

Posted 2010-02-07T19:13:37.977

Reputation: 19 887

Answers

21

Use the command line. For example,

runas /noprofile /user:COMPUTERNAME\USERNAME "C:\MYBAT.bat"

See more about the command in the reference documentation for Runas.

Dan McGrath

Posted 2010-02-07T19:13:37.977

Reputation: 2 946

1At least in WinXP, this also works in the Run box. – pcapademic – 2010-02-07T20:22:19.647

This command line option is not working for me whereas the one below with Right Click Run as is working as expected. What's the difference ? – user85 – 2017-05-11T15:03:13.687

20

To add the Run as... option for .BAT files to the context menu, use the following REG file:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\batfile\shell\runas\command]
@="\"%1\" %*"

Copy the above contents to Notepad, and save the file with .REG extension (say, bat_runas.reg). Right-click the .REG file and choose Merge.

To add the Run as... menu for .CMD files, use this REG file:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\cmdfile\shell\runas\command]
@="\"%1\" %*"

Copy the above contents to Notepad, and save the file with .REG extension (say, cmd_runas.reg). Right-click the .REG file and choose Merge.

Molly7244

Posted 2010-02-07T19:13:37.977

Reputation:

Nifty :-). Any idea why this is not the default? – sleske – 2010-02-08T15:56:23.020

@sleske - This was default in WIndows 2000. It had been removed in XP (and later) for security reasons. added another registry 'hack' to add Run as for CMD files. – None – 2010-02-08T16:03:45.717

@Molly: Thanks. Still I don't get the "security reasons". I can always "Run as" cmd.exe and then run my .bat, that would be the same, wouldn't it? So where's the security problem? – sleske – 2010-02-09T00:41:15.433

1@sleske - sometimes they call it 'User Anxiety', which was the reason for removing the summary from the status bar in Windows 7 when you select a folder. get it? well, i don't ... :P – None – 2010-02-09T00:47:23.047

1

For normal .Exe files you can get Run as Menu by pressing Shift + Rightclick. But this method does not work for .bat and .cmd files. So you have add that option by creating Registry file

  1. Right click on Desktop
  2. click on New
  3. Then select Notepad

  4. Now copy the code written below to this notepad file

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\cmdfile\shell\runas\command] @="\"%1\" %*"

  1. Now press "Ctrl + s" to save this file
  2. save it with "Runas.reg" on desktop or other place you want
  3. Now right click on "Runas.reg" file
  4. click on "Merge"

Programming

Posted 2010-02-07T19:13:37.977

Reputation: 11