I'm trying to write a batch file that will turn Apache off and copy some files. This is all I have in the script at the moment:

"C:\Apache24\bin\httpd.exe" -k stop

If I either run the batch file normally, or right click and select "run as administrator" the console window flashes up and I can briefly see the message "Access denied".

If I instead open a cmd prompt as administrator, and then run the batch file through that, it works perfectly. Please help, I need to do this task on a daily basis and want it to be as quick as a double click

I use Windows 7 Pro btw.

  • 1
    Firstly two things to try/do... Add Pause to the next line of the batch file so you can see what is happening after the command runs. Secondly can you load CMD as administrator and run the command as it is without the batch file simply open CMD as admin and type out the same command.. Let me know how you get on..
    – CharlesH
    Sep 18, 2015 at 6:56
  • This sounds like you have changed the association of the .bat extension or you have malware/virus on your machine. Can you check why the default value is of .BAT under the HKEY_CLASSES_ROOT? I accidentally changed mine once so that I was always editing if I double clicked a batch file and hand to manually run via command prompt till I fixed my registry.
    – netniV
    Sep 18, 2015 at 6:57

3 Answers 3


As per my comment above, if you have messed with your registry association, backup your registry, then save the following as a .reg and apply to reset to W7 defaults for .BAT:

Windows Registry Editor Version 5.00



@="Windows Batch File"






@="\"%1\" %*"













[HKEY_CLASSES_ROOT\batfile\ShellEx\PropertySheetHandlers\ShimLayer Property Page]


Try this, It asks for UAC prompt and runs the command as administrator.

:: BatchGotAdmin
REM  --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"

REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
    echo Requesting administrative privileges...
    goto UACPrompt
) else ( goto gotAdmin )

    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
    echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"

    exit /B

    if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
    pushd "%CD%"
    CD /D "%~dp0"

"C:\Apache24\bin\httpd.exe" -k stop
  • Please quote the essential parts of the answer from the reference link(s), as the answer can become invalid if the linked page(s) change.
    – DavidPostill
    Sep 18, 2015 at 8:15
  • tried, but the code tags got messed up. When i put the script content in code tags, not entire content looked as desired..
    – Bharat G
    Sep 18, 2015 at 8:44
  • Fixed that for you. You should learn how to use markup.
    – DavidPostill
    Sep 18, 2015 at 8:50
  • What you also need to do is add something telling us where you got this from. I've seen lots of variations of this batch file and I don't think you are the author. Please read How to reference material written by others
    – DavidPostill
    Sep 18, 2015 at 8:51
  • Thank you. Definitely, im not its author. I have found that script long back and forgot the source. I use it regularly. It is close to this (but not same) stackoverflow.com/a/25238418 . The one i found exactly had till the line :: ADD COMMANDS TO BE RUN AS ADMIN BELOW. However its hard to find it now, since it had variations.
    – Bharat G
    Sep 18, 2015 at 9:49

Have you tried

runas /noprofile /user:mymachine\administrator cmd

it's under (runas usage:examples) so it should work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.