Page 1 of 1

Run scripts as administrator?

Posted: 08 Aug 2016, 16:30
by Win10isAwful
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!

Re: Run scripts as administrator?

Posted: 08 Aug 2016, 16:34
by VarunAgw
Press Alt+Enter on AutoHotkey.exe -> Compatibility -> Run this program as administrator.

You can also associate .ahk extension with the application if you want

Re: Run scripts as administrator?

Posted: 08 Aug 2016, 16:36
by Capn Odin
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.

Re: Run scripts as administrator?

Posted: 08 Aug 2016, 20:19
by lexikos
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).

Re: Run scripts as administrator?  Topic is solved

Posted: 01 Oct 2018, 12:11
by DRocks
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.

Re: Run scripts as administrator?

Posted: 10 Feb 2021, 13:04
by deanhouseholder
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

Re: Run scripts as administrator?

Posted: 19 Mar 2022, 08:16
by hasnogaems
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?

Re: Run scripts as administrator?

Posted: 19 Mar 2022, 08:23
by boiler
You put it at the top of your AHK script.

Re: Run scripts as administrator?

Posted: 07 Apr 2022, 15:50
by adrianh
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.

Re: Run scripts as administrator?

Posted: 07 Apr 2022, 15:54
by boiler
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.

Re: Run scripts as administrator?

Posted: 07 Apr 2022, 16:10
by adrianh
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.

Re: Run scripts as administrator?

Posted: 07 Apr 2022, 16:14
by boiler
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.

Re: Run scripts as administrator?

Posted: 07 Apr 2022, 16:30
by adrianh
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 20225 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.

Re: Run scripts as administrator?

Posted: 07 Apr 2022, 17:45
by TrebleTA
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?