Click here to Skip to main content
12,887,625 members (41,088 online)
Click here to Skip to main content
Add your own
alternative version


2 bookmarked
Posted 20 Feb 2011

Batch file caveat when running as administrator

, 1 Mar 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
Are you sure your batch file will actually do what you intended?


This tip explains a certain danger in running batch files or command scripts as administrator. It is applicable to systems running Windows Vista or higher, with user account control enabled.


I frequently use batch files to clean up unnecessary files (build targets, settings, etc.), and running it as administrator very nearly wiped out my OS. I'm publishing this to warn others who may commit the same mistake.


What danger?

To put it simply, when you run a batch file as administrator, the working folder changes to %SystemRoot%\system32 (which is usually C:\Windows\system32) [1].


To check it out for yourself, create a simple batch file with the following content, and name it anything you like, say test.bat.


Now run the file by double-clicking it. Note the path displayed, it should be the folder where the file is saved. Now run it again as administrator - right click the file and select run as administrator (or whatever it is in your local language). The path shown will be different.

Press any key to continue...
Figure: Sample output when run as administrator


This is really beyond the scope of the tip, but in case you want to know:

  • cd without parameters prints the current directory (the working directory of the file in this case)
  • pause keeps the console window from closing, so that you can see the above output
  • @ at the beginning of each command prevents the command itself from being echoed (try it without the @ to see what I mean).

Note: The command echo off in the example below does the same thing, it prevents echoing of all commands after it. The @ there prevents that command itself from being echoed.


Not a fix really, just a sanity check you may consider using. If the file is named as above, at you could do the following:

@echo off
if not exist test.bat goto _ERRROR
 rem The actual work of the file is done here
goto _END
echo Invalid directory!


The if statement at the top checks whether the batch file itself exists in the current folder. If you are running as administrator, the working folder will be something else, so the condition will be true, and control will be passed to the _ERROR label, skipping everything.

This is just one example off the top of my head, you may use any variation you like.

That's it, and run safely!


See the alternate below by Reto70 for a better method than my error checking.

[1] Checked on several systems. 64 bit OS may differ, not checked.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Japan Japan
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralReason for my vote of 4 You don't see much on batch files. Pin
Jim Shepherd1-Mar-11 8:26
memberJim Shepherd1-Mar-11 8:26 
GeneralRe: Thanks! (See the alternate for an improved method) Pin
Indivara1-Mar-11 20:09
subeditorIndivara1-Mar-11 20:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170424.1 | Last Updated 2 Mar 2011
Article Copyright 2011 by Indivara
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid