Best way to make ahk scripts run at start-up with admin rights? Topic is solved
Best way to make ahk scripts run at start-up with admin rights? Topic is solved
Best way to make ahk scripts run at start-up with admin rights?
- 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?
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
- MilesAhead
- Posts: 232
- Joined: 03 Oct 2013, 09:44
Re: Best way to make ahk scripts run at start-up with admin rights?
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.
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
name on it and take the blame."
- MilesAhead
Re: Best way to make ahk scripts run at start-up with admin rights?
Yes, but every time I do that it asks me for "what to run this program with"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
Re: Best way to make ahk scripts run at start-up with admin rights?
It should look like:
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
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
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.
- 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?
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:Yes, but every time I do that it asks me for "what to run this program with"
"C:\Program Files\AutoHotkey\AutoHotkey.exe"
and this in the Add arguments field:
"c:\YourAHKfiles\HelloWorld.ahk"
Regards, Joe
-
- Posts: 8
- Joined: 29 Jun 2016, 16:32
Re: Best way to make ahk scripts run at start-up with admin rights?
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
Re: Best way to make ahk scripts run at start-up with admin rights?
Well I have them as the default program ever since I installe the program.JoeWinograd wrote: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:Yes, but every time I do that it asks me for "what to run this program with"
"C:\Program Files\AutoHotkey\AutoHotkey.exe"
and this in the Add arguments field:
"c:\YourAHKfiles\HelloWorld.ahk"
Regards, Joe
So it must be something else.
- 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?
Here are a couple of interesting articles on the matter at The Windows Club:
How to auto start Programs as Administrator in Windows 10
Create elevated Shortcut to run Program bypassing UAC Prompt in Windows 10/8/7
And one at SuperUser:
How to run a program as an administrator at startup on Windows 10?
Regards, Joe
How to auto start Programs as Administrator in Windows 10
Create elevated Shortcut to run Program bypassing UAC Prompt in Windows 10/8/7
And one at SuperUser:
How to run a program as an administrator at startup on Windows 10?
Regards, Joe
Re: Best way to make ahk scripts run at start-up with admin rights?
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?JoeWinograd wrote:Here are a couple of interesting articles on the matter at The Windows Club:
How to auto start Programs as Administrator in Windows 10
Create elevated Shortcut to run Program bypassing UAC Prompt in Windows 10/8/7
And one at SuperUser:
How to run a program as an administrator at startup on Windows 10?
Regards, Joe
Re: Best way to make ahk scripts run at start-up with admin rights?
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.
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.
Re: Best way to make ahk scripts run at start-up with admin rights?
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.
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%"
}
-
- Posts: 4
- Joined: 16 Feb 2018, 01:00
Re: Best way to make ahk scripts run at start-up with admin rights?
For future reference... this did not work for meWesternGun wrote: ↑26 May 2017, 10:27I 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.
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.
Re: Best way to make ahk scripts run at start-up with 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.SenseElation wrote: ↑10 Dec 2020, 17:07For future reference... this did not work for meWesternGun wrote: ↑26 May 2017, 10:27I 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.
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.
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.
-
- Posts: 4
- Joined: 16 Feb 2018, 01:00
Re: Best way to make ahk scripts run at start-up with admin rights?
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).
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.
Re: Best way to make ahk scripts run at start-up with admin rights?
To use Task Scheduler:
1. General: Run with highest privileges
2. Actions:
1. General: Run with highest privileges
2. Actions:
- Program = full path to AutoHotkey.exe
- Argument = full path to your AHK script
-
- Posts: 9
- Joined: 25 Oct 2021, 20:33
Re: Best way to make ahk scripts run at start-up with admin rights?
I like this method, however, when I compile the script with this as the first lines I get this error:ibiscus wrote: ↑05 Jul 2020, 11:52Using 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 do get the UAC prompt and the non-admin version of the script runs but not the one with admin rights ... any thoughts?Error at line 1.
Line Text: MZ
Error: This line does not contain a recognized action.
The program will exit.
Re: Best way to make ahk scripts run at start-up with admin rights?
Compiled scripts would use a different approach. See https://www.autohotkey.com/docs/commands/Run.htm#RunAs.
Re: Best way to make ahk scripts run at start-up with admin rights?
[EDIT] Sorry, I'm confused. What more code thanmikeyww wrote: ↑29 Oct 2021, 09:18Compiled scripts would use a different approach. See https://www.autohotkey.com/docs/commands/Run.htm#RunAs.
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.
Re: Best way to make ahk scripts run at start-up with admin rights?
You can just paste the complete "admin code" (minus MsgBox) at the top of your script. That will work for uncompiled & compiled scripts alike.