Menu

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