Reputation: 157
I have four AHK scripts which run on startup, as they have shortcuts placed in my startup folder. How can I make them run as admin?
What I've tried:
I've tried changing their properties to be run as admin, but they just don't run (when I do that).
I've tried changing their shortcuts' properties to be run as admin, but they too just don't run (when I do that).
I've looked at this reddit post on the topic, but I don't know how to implement that code in the scripts.
I've tried creating a basic C++ application which runs them with system("start path\\to\\script")
, but it wouldn't compile.
In case it matters, the scripts in question are ahk-active-screenshot, CtrlAlt_Switch.ahk, Kill.ahk, and a modified version of this search script.
Upvotes: 3
Views: 5743
Reputation: 176
Add this to top of your scripts:
If (!A_IsAdmin) ; IF NOT Admin
{
Run, *RunAs "%A_ScriptFullPath%" ; Run script as admin
ExitApp ; Exit the current instance running without admin privileges
}
MsgBox, 262144, , Running with admin privileges! ; Continue your code...
A_IsAdmin
is a built in variable that returns 1 if current user has admin rights, otherwise returns 0.!A_IsAdmin
)."%A_ScriptFullPath%"
. The *RunAs
parameter runs the script as admin.ExitApp
exits the current script if it does not have admin rights.Run
command runs the script again with admin rights, now the script has admin rights, it skips the IF
condition and executes code below.Upvotes: 0
Reputation: 11317
You can allow the script to automate administrative programs without running as admin, here're the required steps:
AutoHotkeyU64_UIA.exe
on your disk..ahk
script's properties, change the "Open with" option to the AutoHotkeyU64_UIA.exe
you just found.%APPDATA%\Microsoft\Windows\Start Menu\Programs\Startup
in the file explorer.And you're done.
How does it work?
The "Add 'Run with UI Access' to context menus" option provides a workaround for common UAC-related issues by allowing the script to automate administrative programs without running as admin.
Reference: Run with UI Access.
Upvotes: 7
Reputation: 3994
Find the UIA version of autohotkey on your machine. For me it is at C:\Program Files\AutoHotkey\AutoHotkeyU64_UIA.exe
.
In your startup folder, create a new shortcut (probably one for each of the scripts you want) as follows (replacing paths as appropriate):
This launches your script as an admin using the UIA version of AHK on startup.
Upvotes: 4