Autorun as Administrator in Vista

When asked how to automate the installation of software the other day I was a little dismayed that no-one seemed to have covered the topic of auto-running a batch file from a USB disk as an administrative user.

So this file simply sets up the autorun.inf file which contains exactly what to list in the Vista ‘Autoplay’ dialog when the USB disk is inserted. You will probaly want to rename the ‘Label’ line and perhaps put a pretty icon on your disk called ‘autorun.ico’.

Now create your actual batch file that you wish to run as admin and call it ‘setup.bat’.

As a small note of caution. For some reason Vista does not change to this files active working directory when executing. I can really think of no reason this behavior exists so you might want to add the following rather cryptic command as the first in the batch file. This changes the active directory into the current batch file (in this case ‘setup.bat’) directory.

There obvious when you think about it isn’t it. Shame on you for not knowing about the handy and invisible shell variable “~dp0”. I am sure there is a long and interesting story about why this variable is so called and why it doesnt show up with the rest of things when you type ‘set’. This is why I switched to Linux some time ago.

Anyway, now we’ve done that create a shortcut to the ‘Setup.bat’ file and rename it to ‘setup.lnk’ (remember that the ‘.lnk’ bit will always be invisible to you anyway so when you rename in explorer all you will see is the ‘setup’ bit sans extension).

Open the shortcuts properties and hit the ‘Advanced’ button. Tick the ‘Run as Administrator’ box.

Finally, lets join it all together by creating the ‘Invoke.bat’ file which runs the shortcut (which will in turn run the ‘setup.bat’ program after asking via UAC for administrative permission).