You're a developer/tester, and, say, regularly need to register/unregister COM DLLs. You want to do it from Explorer using "Open with..." menu, but this menu lacks "Run as Administrator" option. Without elevation, your regsvr32 will fail to register anything with an "access denied" error.
So we need to find a way to write batch scripts which will trigger elevation UAC. Regular applications do it placing "requireAdministrator" attribute in a manifest, but this is not an option for command batches.
Here is a solution
1. Create an empty file (let's name it “elevated.cmd”), save it in a place write-accessible for your batch scripts. For example, let it be a “Tools” folder on your desktop, so the path will be “%userprofile%\Desktop\Tools”
2. Create a shortcut to the file in the same folder. We'll call it “RunElevated.lnk”.
3. Edit shortcut properties, check the “Run as administrator flag”.
Everything is ready. These two files will be used as a launchpad for all our elevated commands.
Now let's use it. For example, for the DLL [un]registration task, let's create a “Register DLL.cmd” batch with a following sequence:
@set tooldir=%userprofile%\Desktop\Tools
@echo regsvr32.exe "%~1" > "%tooldir%\elevated.cmd"
@echo pause >> "%tooldir%\elevated.cmd"
@"%tooldir%\RunElevated.lnk"
@echo. > "%tooldir%\elevated.cmd"
So we're just deploying the code we’re going to elevate into the “elevated.cmd” launchpad (just @echo-ing it), pulling a trigger using the link (system will ask user to elevate access rights because of the flag we set in the LNK), pausing to see results of the launch in console (optional), then cleaning the launchpad for next uses.This example is not safe for concurrent access, so if you need it, you should provide some synchronization.