I like to run the search program Everything (link valid 6/4/2014 but check techsupportalert.com for any updates) but it is a stand-alone executable downloaded from the internet so it triggers a UAC (User Account Control) warning.
No biggie, but i also want it to start automatically when windows starts. And i dont want to have to click UAC or have my customers click UAC every time they start windows.
I also might want to put commands that must be elevated in batch jobs (.bat files) when there’s no one there to click YES to the UAC.
The workaround is to go thru task scheduler. There you can set up a task to start with elevated privileges which won’t trigger a UAC.
The article at http://www.winhelponline.com/blog/run-programs-elevated-without-getting-the-uac-prompt/ contains the basic, but forgets some points.
My edits are in green:
Creating a Scheduled Task
1. Open Task Scheduler by clicking the Windows Orb and typing “task” and selecting “Task Scheduler” from the search results, or from Control Panel or by running the command control schedtasks
from Start, Search box.
2. Left-click Task Scheduler Library category in the left to select it (make it highlighted; or else you can’t do “New Folder” below)
3. Right-click “Task Scheduler Library” category in the left, and choose New Folder
4. Name the folder as MyApps (or whatever name you choose; you’ll use the same name again, below)
5. Click the little triangle to the left of “Task Scheduler Library” to display the list of folders beneath it, including our newly created MyApps folder.
Select the MyApps folder (or whatever you named it) by left clicking on it once.
6. In the Actions pane on the right, click Create Task…
7. Type a name for the task that you want to create. You’re going to have to type this again later, so for convenience, keep it short and dont use any spaces.
8. Enable the option Run with highest privileges. This is an important step. In fact the key to the whole thing.
8.a. (FYI The “Hidden” checkbox refers to only viewing tasks in the task scheduler; there is way to show or now show hidden tasks when in task scheduler; This Hidden settings does not make the task hidden in any way when it runs.)
9. Select the Action tab
10. Click New
11. Click Browse… to select the program (Example: Regedit.exe) you want to run, and mention the parameters (called arguments in task scheduler). required if any, for the application. (For example, to run a .REG file, select Regedit.exe and mention the parameter (argument) as “/s filename.reg” without the quotes. Another example: my Everything program takes a -startup argument to start in the system tray rather than with a window.)
To run Services MMC applet, browse and select MMC.EXE and type services.msc in the Add arguments (optional) field.
12. Select the Conditions tab
12.a. UNcheck “Start the task only if the computer is on AC power”
13. Select the Settings tab
13.a UNcheck “Stop the task if it runs longer than”
13.b at the bottom there is “If the task is already running, then the following rule applies:” You can choose whatever you want here.
If when it’s running you want to not start another one, select “Do not start a new instance”.
If when it’s running you want might want start another one running simultaneously, select “Run a new instance in parallel”.
If when it’s running you want to end the one that’s currently running, and start another one running, select “Stop the existing instance”.
I suppose “Queue a new instance” is helpful, but i can’t think of an example right now.
Note, you can export this to an xml file (see below) and then import it into another system
Creating a Scheduled Task via cmd line schtasks
(NOTE: this method is NOT AS GOOD–see below)
Alternatively, you can create a task via the cmd line schtasks
:
schtasks /create /sc once /tn cmd_elev /tr cmd /rl highest /st 00:00
schtasks
- invoke the schtasks.exe cmd line program
/create
- schtasks can also query, delete, etc; we want to create a task
/sc once
- how often to run; we’re not creating a repetitive task; in fact we dont want to run it at all, just set it up to be run manually, but that option does not appear to be available, so “once” is the closest (see explanation at /sc starttime)
/tn cmd_elev
/tn
stands for Task Name and you can name it anything you want (but you have to use the same name when you later invoke/run
/tr cmd
- the command to run, in this case the
cmd.exe
program, aka command prompt /rl highest
- Why we came to the party.
/rl
stands for Run at privilege Level; and we want thehighest
priv level /st 00:00
/st
stands for Start Time; dont want a start time, but it appears to require one. When you create thru the GUI you dont have to have a trigger at all, but via cmd line you seem to (or was it just me?). I just put in 00:00 (format is HH:MM) which is never in the future, which means it’ll trigger a warningWARNING: Task may not run because /ST is earlier than current time.
Yeah, that’s what i want. (NOTE: love those microsoft guys: it’s a nit, but when the time is EQUAL to the current time, you get the warning)
If all goes well, you should see
SUCCESS: The scheduled task "cmd_elev" has successfully been created.
or whatever name you called it.
If you re-run the line (let’s say you were experimenting 😉 you will get the warning
WARNING: The task name "cmd_elev" already exists. Do you want to replace it (Y/N)? y
You can safely say Y to this.
Namely,
- “start the task only if the computer is on ac power” will be checked in the conditions tab; not ideal
- “stop the task if it runs longer than [time]” where time defaults to “3 days” will be checked in the setting tab; not ideal
Launching a Scheduled Task item manually
To run a scheduled task item manually, use the schtasks.exe
command-line tool that comes with Windows. For example, to launch the Services console task that you already created, use the following command:
SCHTASKS.EXE /RUN /TN MyApps\REGEDIT
Note: Where MyApps\REGEDIT is the name you chose for the folder and Taskname. You’ll need to enclose the task name within double-quotes if the task name contains blank spaces in between. (Example: SCHTASKS.EXE /RUN /TN folder\“Name of the Task”) If the folder has spaces, you’ll have to enclose that in double-quotes; perhaps best to enclose the whole thing. (Example: SCHTASKS.EXE /RUN /TN “folder\Name of the Task”)
Creating Shortcuts to run each Task
You can create a Desktop shortcut for each scheduled task item you’ve created earlier. Right-click on the Desktop and choose New, Shortcut. Type the command-line (say, SCHTASKS.EXE /RUN /TN MyApps\REGEDIT). Mention a name for the shortcut and click Finish.
Run the task minimized
As Schtasks.exe
is a console utility, you’ll see the Command Prompt window opening and closing quickly whenever you run the shortcut. So, you may configure the shortcut to run in a minimized window state, in the the shortcut properties.
- Right-click on the shortcut and click Properties.
- In the Run drop-down options, change the selection from “Normal Window” to Minimized.
- Click OK.
Note: In the shortcut properties, you may want to click Change Icon and assign appropriate icon for the shortcut. The icons should be present inside the executable itself, in most cases. For Regedit.exe, browse to Regedit.exe and choose an icon. You may also browse the shell32.dll and imageres.dll files for additional icons.
xml file
Once you set up a task the way you like it via the GUI (recommended method), you can export it to an xml file so that it’s easier to set up on the next system.
Here’s my xml file for (Search) Everything. Filename ends in .xml.txt so change it to just .xml befor importing to task scheduler. start_search_everything.xml.txt
Other Methods
There are at least three other compelling methods.
The first two come from
- stackoverflow : Open an elevated cmd using command line for Windows
1. NirSoft NirCmd
nircmdc
has an elevate
cmd
nircmdc elevate cmd
2. SysInternals PsExec
Part of PsTools suite.
psexec.exe -accepteula -h -u “$username” -p “$password” cmd.exe
The “-h” switch is the one doing the magic:
-h If the target system is Vista or higher, has the process run with the account’s elevated token, if available.
The third comes from
- Matt’s answer to stackoverflow question : How can I auto-elevate my batch file, so that it requests from UAC administrator rights if required?
3. vbs or PowerShell to elevate
key lines are to use the visual basic script / vbscript command UAC.ShellExecute
to perform the elevation, create a new cmd (command prompt), and re-run the original bat file, now elevated. Does that by writing a .vbs file:
ECHO UAC.ShellExecute "!batchPath!", "ELEV", "", "runas", 1 >> "%temp%\OEgetPrivileges.vbs" "%temp%\OEgetPrivileges.vbs"
Or, you can use PowerShell to elevate:
if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( powershell "saps -filepath %0 -verb runas" >nul 2>&1)
Elevated command prompt and mapped drives
One problem you might encounter when using this method if you’re running a batch file (.bat) in elevated mode, is: Mapped drives (incl net use) missing from elevated processes eg UAC cmd run as administrator. Fortunately, there’s some solutions in that blog post.