Run scripts as administrator? Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
Win10isAwful

Run scripts as administrator?  Topic is solved

08 Aug 2016, 16:30

I recently "upgraded" to Windows 10, and now I'm running into the frustrating problem of scripts not interacting with other programs/apps unless the script is run as administrator.

In the past, I've always kept my scripts on my desktop as .txt, and when I want to execute them, I right-click, Open With, and select AHK. This doesn't work now, since that action will not run the script as administrator.

How can I set up my computer such that I can quickly run scripts as administrator?

Thanks for your help!
VarunAgw
Posts: 25
Joined: 05 Jan 2016, 11:51
GitHub: VarunAgw

Re: Run scripts as administrator?

08 Aug 2016, 16:34

Press Alt+Enter on AutoHotkey.exe -> Compatibility -> Run this program as administrator.

You can also associate .ahk extension with the application if you want
User avatar
Capn Odin
Posts: 1352
Joined: 23 Feb 2016, 19:45
GitHub: CapnOdin
Location: Denmark

Re: Run scripts as administrator?

08 Aug 2016, 16:36

Try changing the extension to .ahk, if that dossne't work you will need to change the registry, it can be done using regedit, search for it in the start menu.
Please excuse my spelling I am dyslexic.
lexikos
Posts: 8549
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Run scripts as administrator?

08 Aug 2016, 20:19

I would not recommend changing compatibility settings of AutoHotkey.exe.

Running as administrator is not the only solution; see the FAQ: How do I work around problems caused by User Account Control (UAC)?

A script can also run itself as admin by using Run *RunAs "%A_AhkPath%" "%A_ScriptFullPath%". (A_AhkPath is usually optional if the script has the .ahk extension.) You would typically check if not A_IsAdmin first.

If you want a "Run as administrator" option for txt files, you can try copying the Shell\RunAs\Command subkey of HKCR\AutoHotkeyScript into the text file key (usually HKCR\txtfile), but I think you're better off just using the .ahk extension. If you want Edit to be the default (for double-click), you can change the default value of HKCR\AutoHotkeyScript\Shell to Edit (it is usually Open).
DRocks
Posts: 565
Joined: 08 May 2018, 10:20

Re: Run scripts as administrator?

01 Oct 2018, 12:11

lexikos wrote:A script can also run itself as admin by using Run *RunAs "%A_AhkPath%" "%A_ScriptFullPath%". (A_AhkPath is usually optional if the script has the .ahk extension.) You would typically check if not A_IsAdmin first.
works perfectly thanks for this!

Code: Select all

#SingleInstance Force
SetWorkingDir %A_ScriptDir%
if not A_IsAdmin
	Run *RunAs "%A_ScriptFullPath%" ; (A_AhkPath is usually optional if the script has the .ahk extension.) You would typically check  first.
deanhouseholder
Posts: 1
Joined: 21 Feb 2017, 18:19

Re: Run scripts as administrator?

10 Feb 2021, 13:04

I wanted something similar, but I wanted to keep my main ahk script running as an unprivileged/unelevated script, but kick off an elevated batch script that would run some command prompt command lines, and close.

I settled on this solution. In my .ahk file:

Code: Select all

; Restart Hyper-V Services (To free up vmmem.exe's memory after closing Docker)
; Alt+F12
; *RunAs syntax will prompt for admin privileges
!F12::
  try {
    Run *RunAs C:\Utils\RestartService\Restart-HVHost-Service.bat
  } catch {
    MsgBox, Could not obtain admin privileges.
  }
Return
In my Restart-HVHost-Service.bat file:

Code: Select all

@echo off
echo Restarting HV Host Service
sc stop HVHost >NUL
sc stop vmcompute >NUL
sc start HVHost >NUL
sc start vmcompute >NUL
hasnogaems
Posts: 2
Joined: 27 Mar 2021, 19:38

Re: Run scripts as administrator?

19 Mar 2022, 08:16

DRocks wrote:
01 Oct 2018, 12:11

Code: Select all

#SingleInstance Force
SetWorkingDir %A_ScriptDir%
if not A_IsAdmin
	Run *RunAs "%A_ScriptFullPath%" ; (A_AhkPath is usually optional if the script has the .ahk extension.) You would typically check  first.
You put this code in ahk script or in some bat file?
User avatar
boiler
Posts: 12928
Joined: 21 Dec 2014, 02:44

Re: Run scripts as administrator?

19 Mar 2022, 08:23

You put it at the top of your AHK script.
adrianh
Posts: 126
Joined: 28 Jul 2014, 15:34

Re: Run scripts as administrator?

07 Apr 2022, 15:50

lexikos wrote:
08 Aug 2016, 20:19
Running as administrator is not the only solution; see the FAQ: How do I work around problems caused by User Account Control (UAC)?
How do I work around problems caused by User Account Control (UAC)? wrote:Common workarounds are as follows:
  • Enable the Add 'Run with UI Access' to context menus option in AutoHotkey Setup. This option can be enabled or disabled without reinstalling AutoHotkey by re-running AutoHotkey Setup from the Start menu. Once it is enabled, launch your script file by right-clicking it and selecting Run with UI Access, or use a command line like "AutoHotkeyU32_UIA.exe" "Your script.ahk" (but include full paths).
(Emphasis mine)

I don't see any way of re-running AutoHotkey Setup from the Start menu. I've only installed this in the last month.

EDIT: I found the installer I had dled and reran it. Then specified Modify and got to the screen that has a checkbox to install the UIA. On a further note, the name of the default startup script is AutoHotkeyU64_UIA.ahk and it must have at least a UTF-8 BOM attached to it.
Last edited by adrianh on 07 Apr 2022, 16:06, edited 1 time in total.
User avatar
boiler
Posts: 12928
Joined: 21 Dec 2014, 02:44

Re: Run scripts as administrator?

07 Apr 2022, 15:54

Does your Start menu have an "AutoHotkey" group (folder) in it? If so, does it have "AutoHotkey Setup" inside of that group? If not, then your installation was not standard or complete, so download it from the website and install it again.
adrianh
Posts: 126
Joined: 28 Jul 2014, 15:34

Re: Run scripts as administrator?

07 Apr 2022, 16:10

It didn't have that "AutoHotkey Setup" inside the "AutoHotkey" group. The install was IIRC, standard and complete. There were no msgs to indicate otherwise.
User avatar
boiler
Posts: 12928
Joined: 21 Dec 2014, 02:44

Re: Run scripts as administrator?

07 Apr 2022, 16:14

Apparently, it wasn't complete. What does that group in your Start menu contain?

I also have a machine where AHK was installed within the last month, and the Start menu AutoHotkey group contains, AutoHotkey, AutoHotkey Help File, AutoHotkey Setup, Convert .ahk to .exe, Website, and Window Spy.
adrianh
Posts: 126
Joined: 28 Jul 2014, 15:34

Re: Run scripts as administrator?

07 Apr 2022, 16:30

It may be possible that I missed it. After the modified install, I do see an AutoHotkey Setup item. The icon next to it looks like a text document which might be why I may have disregarded it. I doubt it, but I guess it might be possible.
image.png
image.png (51.67 KiB) Viewed 6663 times
Just wanted to report this as a potential install issue.

In any case, might be a good idea to change the icon to a gear for clarity. Maybe a world icon or something for the website too.
TrebleTA
Posts: 134
Joined: 20 Nov 2021, 06:44

Re: Run scripts as administrator?

07 Apr 2022, 17:45

So your script is running as a admin but you want to run programs as the current user?
Or you running as a standard user looking to run admin?

Return to “Ask For Help”

Who is online

Users browsing this forum: 1337ingDisorder, Ahmed142, altokers, bapl, Chunjee, Destructoid, Flipeador, MagEpub, mikeyww, Smile_, wgf4242 and 59 guests