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: 1643
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: 1643
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%"
}
SenseElation
Posts: 4
Joined: 16 Feb 2018, 01:00

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

10 Dec 2020, 17:07

WesternGun wrote:
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.
For future reference... this did not work for me :(

It works up until you set the script (instructions unclear as to whether this person meant AutoHotkey the programme or the exe of your file) to run as Admin. Then it will not launch. It will launch if it's not set to admin rights.
SOTE
Posts: 1193
Joined: 15 Jun 2015, 06:21

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

11 Dec 2020, 14:55

SenseElation wrote:
10 Dec 2020, 17:07
WesternGun wrote:
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.
For future reference... this did not work for me :(

It works up until you set the script (instructions unclear as to whether this person meant AutoHotkey the programme or the exe of your file) to run as Admin. Then it will not launch. It will launch if it's not set to admin rights.
I think what WesternGun was referring to is the actual properties of the AutoHotkey.exe file. There is a setting under Compatibility for "Run this program as a Administrator". In this way, a person would be setting AutoHotkey.exe to always run with administrator permission. If the .ahk extension is always associated with the AutoHotkey.exe, then when you have your script run at startup, it will be as admin.

The thing about that method is that all .ahk scripts will run as admin too, unless the person does something funky like have the script run with a copy of the AutoHotkey.exe with a different name and the script with a different extension. Maybe try, AutoFunky.exe (renamed AutoHotkey.exe) -> ThatScript.funk (script w/ different extension). Though not sure if doing such is a good thing.

Probably a better way, using that method, is to compile the script into an executable. Then go to its Properties, then Compatibility, then click the "Run this program as a Administrator" check box, and see how that goes. This way such a change affects only that specific program.
SenseElation
Posts: 4
Joined: 16 Feb 2018, 01:00

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

15 Dec 2020, 07:26

SOTE, thanks for your reply. Actually I did make the script itself into an exe because I imagined it would easier run with admin rights.

I tried it all three ways:

1) Running AutoHotkey.exe as admin, the script as admin
2) Autohotkey.exe admin, script non-admin
3) Autohotkey.exe non-admin, script admin

What happens is that the script will not launch at startup if the script itself is set as admin (1 & 3).


Incidentally, I have tried the Task Scheduler suggested solution before and I haven't gotten it to work (nothing happens).
Last edited by SenseElation on 15 Dec 2020, 12:11, edited 1 time in total.
User avatar
mikeyww
Posts: 3902
Joined: 09 Sep 2014, 18:38

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

15 Dec 2020, 08:47

To use Task Scheduler:

1. General: Run with highest privileges

2. Actions:
  • Program = full path to AutoHotkey.exe
  • Argument = full path to your AHK script

Return to “Ask For Help”

Who is online

Users browsing this forum: Aggronaught, Bing [Bot], elvo11, JoeWinograd, keylo, mikeyww, TAC109 and 53 guests