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

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
friendly

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

Post by friendly » 12 Aug 2016, 08:45

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

User avatar
JoeWinograd
Posts: 2198
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

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

Post by JoeWinograd » 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?

Post by MilesAhead » 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?

Post by Guest » 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?

Post by Rami » 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: 2198
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

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

Post by JoeWinograd » 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

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

Post by Kahless2000 » 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?

Post by friendly » 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: 302
Joined: 23 Jan 2014, 12:05

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

Post by Skrell » 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?

Post by WesternGun » 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: 4
Joined: 09 Feb 2020, 12:57

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

Post by ibiscus » 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?

Post by SenseElation » 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: 1426
Joined: 15 Jun 2015, 06:21

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

Post by SOTE » 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?

Post by SenseElation » 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: 26871
Joined: 09 Sep 2014, 18:38

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

Post by mikeyww » 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

Technomage Awunes
Posts: 9
Joined: 25 Oct 2021, 20:33

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

Post by Technomage Awunes » 29 Oct 2021, 07:52

ibiscus wrote:
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%"
}
I like this method, however, when I compile the script with this as the first lines I get this error:
Error at line 1.

Line Text: MZ
Error: This line does not contain a recognized action.

The program will exit.
I do get the UAC prompt and the non-admin version of the script runs but not the one with admin rights ... any thoughts?


LAPIII
Posts: 668
Joined: 01 Aug 2021, 06:01

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

Post by LAPIII » 19 Jan 2022, 20:37

mikeyww wrote:
29 Oct 2021, 09:18
Compiled scripts would use a different approach. See https://www.autohotkey.com/docs/commands/Run.htm#RunAs.
[EDIT] Sorry, I'm confused. What more code than
Is this what I what I'd need to start a script with so that I don't need to manually run as administrator:

Code: Select all

if not (A_IsAdmin(full_command_line, " /restart(?!\S)"))
        else
            Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%"


Last edited by LAPIII on 19 Jan 2022, 20:42, edited 1 time in total.

User avatar
mikeyww
Posts: 26871
Joined: 09 Sep 2014, 18:38

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

Post by mikeyww » 19 Jan 2022, 20:42

You can just paste the complete "admin code" (minus MsgBox) at the top of your script. That will work for uncompiled & compiled scripts alike.

Post Reply

Return to “Ask for Help (v1)”