Sam
Sam

Reputation: 157

Run AHK script as admin on startup

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

Answers (3)

Mister Robato
Mister Robato

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...
  1. A_IsAdmin is a built in variable that returns 1 if current user has admin rights, otherwise returns 0.
  2. It first checks if user is NOT admin (!A_IsAdmin).
  3. If user is not admin, it runs the same script which it gets the full path with the built in variable "%A_ScriptFullPath%". The *RunAs parameter runs the script as admin.
  4. ExitApp exits the current script if it does not have admin rights.
  5. The 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

Wenfang Du
Wenfang Du

Reputation: 11317

You can allow the script to automate administrative programs without running as admin, here're the required steps:

  1. When installing AHK, check the "Add 'Run with UI Access' to context menus" option.
  2. After installation, find AutoHotkeyU64_UIA.exe on your disk.
  3. Open the .ahk script's properties, change the "Open with" option to the AutoHotkeyU64_UIA.exe you just found.
  4. Create a shortcut to this script.
  5. Open %APPDATA%\Microsoft\Windows\Start Menu\Programs\Startup in the file explorer.
  6. Move that shortcut to this folder.

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

HaveSpacesuit
HaveSpacesuit

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):

  • Target: "C:\Program Files\AutoHotkey\AutoHotkeyU64_UIA.exe" "D:\path\to\script.ahk"
  • Start in: "C:\Program Files\AutoHotkey"

This launches your script as an admin using the UIA version of AHK on startup.

Upvotes: 4

Related Questions