Best way to make ahk scripts run at start-up with admin rights?

Get help with using AutoHotkey and its commands and hotkeys
User avatar
JoeWinograd
Posts: 1545
Joined: 10 Feb 2014, 20:00

Re: Best way to make ahk scripts run at start-up with admin rights?

12 Aug 2016, 09:14

I don't know if it's the "best" way, but one way is to create a task in the Task Scheduler, set the condition in the Triggers tab to begin the task "At startup", and tick the box in the General tab that says "Run with highest privileges" (that means run it as Administrator, i.e., elevated). Regards, Joe
User avatar
MilesAhead
Posts: 232
Joined: 03 Oct 2013, 09:44

Re: Best way to make ahk scripts run at start-up with admin rights?

12 Aug 2016, 10:52

Joe's approach above is the recommended method. Windows considers it a security issue if you try to run as Admin during login. The other legit way of doing it, which is not all that applicable to scripting programs, is to run the exe as a service. That way it runs as SYSTEM.

Me, I hate using Task Scheduler. So what I do to launch all my As Administrator compiled scripts is to launch them all via an ahk program with the requiresadmin attribute. I have an icon to this program on the desktop and make sure it is not convered by any windows during startup. Then I run a non-admin ahk script to double click it. Cheating I know. But it's just a macro. I don't need to run any of these scripts As Admin on W7 or earlier. It may be due to the fact that on Windows 8.0 I have on my Laptop, the desktop to wrapped in some other layer of whatnot. So scripts that do stuff like watch the active window or move windows around on screen don't seem to work as well as on earlier Windows versions.
"My plan is to ghostwrite my biography. Then hire another writer to put his
name on it and take the blame."

- MilesAhead
Guest

Re: Best way to make ahk scripts run at start-up with admin rights?

12 Aug 2016, 11:48

JoeWinograd wrote:I don't know if it's the "best" way, but one way is to create a task in the Task Scheduler, set the condition in the Triggers tab to begin the task "At startup", and tick the box in the General tab that says "Run with highest privileges" (that means run it as Administrator, i.e., elevated). Regards, Joe
Yes, but every time I do that it asks me for "what to run this program with"
Rami
Posts: 55
Joined: 19 May 2016, 07:44

Re: Best way to make ahk scripts run at start-up with admin rights?

12 Aug 2016, 12:27

It should look like:

Code: Select all

RunWait, %A_WinDir%\System32\schtasks.exe /create /TN [TaskName] /TR [Path\To\MyScript.exe] OR ["c:\program files\AutoHotkey\autohotkey.exe" "PathTo\MyScript.ahk"] /RL HIGHEST /SC ONLOGON
Edit*: Added OR , and my question :)
For more info see Schtasks.exe

But the question how can I specify the user? If I want to run the script when a specific user logs in. Because the above command will schedule task to run at log on of ANY USER
Last edited by Rami on 12 Aug 2016, 12:29, edited 2 times in total.
User avatar
JoeWinograd
Posts: 1545
Joined: 10 Feb 2014, 20:00

Re: Best way to make ahk scripts run at start-up with admin rights?

12 Aug 2016, 12:28

Yes, but every time I do that it asks me for "what to run this program with"
That's because you didn't run the installer, so Windows doesn't know what program owns the AHK file type. When you run the installer, it associates AHK files with AutoHotkey.exe. So you can either (1) run the installer or (2) manually associate AHK files with AutoHotkey.exe (for example, by right-clicking in your file manager, then Open with and Choose default program; or via Control Panel>Default Programs) or (3) fully qualify the path in the Actions tab, i.e., Start a program should be something like this in the Program/script field:

"C:\Program Files\AutoHotkey\AutoHotkey.exe"

and this in the Add arguments field:

"c:\YourAHKfiles\HelloWorld.ahk"

Regards, Joe
Kahless2000
Posts: 8
Joined: 29 Jun 2016, 16:32
Google: Tim Bailey

Re: Best way to make ahk scripts run at start-up with admin rights?

12 Aug 2016, 14:04

Windows 10 will not allow anything to run at startup in "Administrator Mode" from the startup menu. To get around this I have always made my AHK script into an executable and manually set that program to run in Administrator Mode from the Properties menu. Then I create another AHK script that just runs the first program. ( I use it like a shortcut) and I make it executable as well. This "shortcut script" I place in the "COMMON STARTUP FOLDER" on Windows 10 it can be found here: C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp I will post some example code of how I do the "Shortcut Script". The key to it all is , you are placing the "shortcut script" which does not have admin rights in the startup folder that "does not accept anything with admin rights" but it opens and runs a program that "Does Have Admin Rights". Anyway it works well for me.

Code: Select all

#SingleInstance force
;~ #Persistent
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

;Removes Sys Tray Icon
#NoTrayIcon
Gui,+ToolWindow
DetectHiddenWindows, on


;--------This Program will start a the Virtual Com Port Program in the Program files menu that has already been manually confiqured to  "Run as an Administrator".



Run, C:\Program Files\USR-VCOM\USR-VCOM.exe,Min



Sleep, 10000



	
	
ExitApp

friendly

Re: Best way to make ahk scripts run at start-up with admin rights?

12 Aug 2016, 14:10

JoeWinograd wrote:
Yes, but every time I do that it asks me for "what to run this program with"
That's because you didn't run the installer, so Windows doesn't know what program owns the AHK file type. When you run the installer, it associates AHK files with AutoHotkey.exe. So you can either (1) run the installer or (2) manually associate AHK files with AutoHotkey.exe (for example, by right-clicking in your file manager, then Open with and Choose default program; or via Control Panel>Default Programs) or (3) fully qualify the path in the Actions tab, i.e., Start a program should be something like this in the Program/script field:

"C:\Program Files\AutoHotkey\AutoHotkey.exe"

and this in the Add arguments field:

"c:\YourAHKfiles\HelloWorld.ahk"

Regards, Joe
Well I have them as the default program ever since I installe the program.

So it must be something else.
Skrell
Posts: 166
Joined: 23 Jan 2014, 12:05

Re: Best way to make ahk scripts run at start-up with admin rights?

21 Nov 2016, 19:36

So based on this post I assume there is still no easier way to add something to a script to have it automatically launch itself with elevated privileges? :(
WesternGun

Re: Best way to make ahk scripts run at start-up with admin rights?

26 May 2017, 10:27

I just tested in my windows 10 home and my way works. Here's what I did:

1. Set in the properties of AutoHotKey.exe to run as Admin. (properties - compatibility).
2. Link your script to the exe, to make it run default by AHK.
3. Add in registry this script. (HKEY_LOCAL_MACHINE\software\Microsoft\Windows\CurrentVersion\Run, create a string, value is the path to your script)

That works.
ibiscus
Posts: 2
Joined: 09 Feb 2020, 12:57

Re: Best way to make ahk scripts run at start-up with admin rights?

05 Jul 2020, 11:52

Using Joe's method I had trouble with the #include command. You must not forget to fill the Start in (optional) field in the Edit Action dialog for everything to work correctly.

If you don't mind the UAC prompt you can simply add the following at the start of your script as described by Lexikos here.

Code: Select all

If (!A_IsAdmin){
Run *RunAs "%A_AhkPath%" "%A_ScriptFullPath%"
}

Return to “Ask For Help”

Who is online

Users browsing this forum: A_AhkUser, boiler, enrica, Google [Bot], keedooh, Tomo2407 and 46 guests