Current Directory in Elevated Batch Scripts in Windows 8

I am seeing a difference between elevated batch scripts in Windows 7 and Windows 8.  If you copy the following in the a batch script and run in either version, you get the current directory.  However if you right-click Run As Administrator, you see the current directory in Windows 7 and “C:Windowssystem32” in Windows 8:

@echo off
echo Current path is %cd
%pause

Fortunately there is an easy workaround by changing the current directory to the path of the current script (parameter zero):

@echo off
echo Current path is %cd%
echo Changing directory to the path of the current script
cd %~dp0
echo Current path is %cd%
pause

I’m using this in context of the StackOverflow answer showing how to auto elevate a script:
How can I auto-elevate my batch file, so that it requests from UAC admin rights if required?

I added this info in case anyone else had the same issue: http://stackoverflow.com/a/18037959/221018

 

Advertisement