Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


How to make batch scripts only run as Administrator

Published on
3,125 Points
Last Modified:
This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.

I am not an hardcore scripter and this is probably not for hardcore scripters.

I have for quite some time searched for a handy way of not accidentally running batch scripts that requires administrative previlieges, WITHOUT it being able to run the first few commands that doesn't need administrative previlieges, and just ends up messning things up.

Anyway, here it is, I did not find it on google, I scavanged it from a batch script that originally started a program.

Where it says "Insert commands here" at row 7 is where your code goes. To clarify, you delete "Insert commands here" and put your code where that line was.

@echo off

::call :check_Permissions
call :checkperms
if "%ADMIN%"=="FAIL" goto :EOF

Insert commands here

:: net session >nul 2>&1
:: sfc 2>&1 | find /i "/SCANNOW"
setlocal enabledelayedexpansion
fsutil dirty query %systemdrive% >nul
if not errorLevel 1 (
echo Administrative permissions confirmed.
) else (
echo You need to run as Administrator [use right-click - Run as administrator].
echo If you ran as Administrator, please check your PATH environment variable includes the Windows\system32 folder.
color cf
goto :eof

set randname=%random%%random%%random%%random%%random%
md %windir%\%randname% 2>nul
if %errorlevel%==0 (echo Administrative permissions confirmed.
goto end)
if %errorlevel%==1 (
echo You need to run as Administrator [use right-click - Run as administrator].
color cf
goto end)
goto checkperms
rd %windir%\%randname% 2>nul
goto :eof

If you found any problems in my grammar or in the commands, just tell me, thanks!

Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Join & Write a Comment

How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month