Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

Run as Administrator (XP/Vista/7) A_IsAdmin Params [Lib]

  • Please log in to reply
49 replies to this topic
  • Members
  • 179 posts
  • Last active: Apr 24 2014 03:24 PM
  • Joined: 05 Feb 2007
Thanks... that helps clear things up. :)

  • Members
  • 499 posts
  • Last active: Mar 14 2014 03:19 AM
  • Joined: 25 Jul 2006
I'm wanting to use this in an XP environment, where the user is a Power User. I use a compiled script, and my code is below:


Run, cscript.exe ToggleNIC.wsf, Hide
However, when I run it as a generic user, it still prompts me for a admin password. I want it to just work and elevate itself. How can I get around this?

  • Moderators
  • 717 posts
  • Last active: Jul 31 2014 08:27 PM
  • Joined: 27 Mar 2008
I assume you're running Vista/7.

The script has the same rights as the user who ran it.

If you run it as a "generic user" then it will have the same rights as that user, and cannot be elevated to administrator all by itself if you have UAC turned on. This is a security feature.

This script only asks for administrator rights. If the only way windows will give it admin rights is to ask for an admin password then that's what's going to happen.

  • Members
  • 499 posts
  • Last active: Mar 14 2014 03:19 AM
  • Joined: 25 Jul 2006
No, I want to run this on a Windows XP machine. It will be run by a generic user. It needs to run with administrator privileges as it will enable/disable the wifi adapter.

If this will not work, any ideas as to what might? Nothing in my research is finding something like this.

  • Members
  • 4368 posts
  • Last active: Aug 09 2015 02:36 AM
  • Joined: 26 Dec 2010
You have to realize what you're asking. You want something tha bypasses security restrictions and spontaneously elevates itself. Do you understand that if that were possible, all kinds of malware would use it?

  • Members
  • 499 posts
  • Last active: Mar 14 2014 03:19 AM
  • Joined: 25 Jul 2006
Yes and no. I want it to do that but I have the credentials I can supply the script itself to have that admin access. My thought is to have it authenticate, elevate then run. Perhaps this isn't the right function to look at, but I'm open to suggestions.

  • Members
  • 132 posts
  • Last active: Jan 04 2015 01:02 AM
  • Joined: 03 Jun 2008
You have to use a different approach for something like that.

You can give the generic user the power to run an executable as administrator. Give a look at runasSpc <!-- m -->http://www.robotroni...runasspcEn.html<!-- m -->
All my scripts/snippets are released under the WTFPL: http://sam.zoy.org/wtfpl/COPYING

  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
Superb, ABCza, it works perfectly and removes a bad thorn in my foot I had ever since I switched at work (where I am not admin by default) to a Windows 7 computer. I was so tired to switch to the admin account, on a different domain (lot to type), with this stupid Computer Misuse Act screen!
And it preserves confidentiality. Better than RunAs which apparently stopped to work in Windows 7.

Now, I can run my AHK script installing/starting/stopping/uninstalling a service without having to do all this finger work...
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

  • Members
  • 95 posts
  • Last active: Jan 04 2013 03:35 PM
  • Joined: 18 Jul 2011
I was looking for this script, but the link on the first page is broken.
Does anyone have the latest version of the script?

Alternatively, is the full code for the script just the following lines that I've copied from the first page? (I don't understand DllCall well enough to know)
Loop, %0%  ; For each parameter:
    param := %A_Index%  ; Fetch the contents of the variable whose name is contained in A_Index.
    params .= A_Space . param
ShellExecute := A_IsUnicode ? "shell32\ShellExecute":"shell32\ShellExecuteA"
if not A_IsAdmin
    If A_IsCompiled
       DllCall(ShellExecute, uint, 0, str, "RunAs", str, A_ScriptFullPath, str, params , str, A_WorkingDir, int, 1)
       DllCall(ShellExecute, uint, 0, str, "RunAs", str, A_AhkPath, str, """" . A_ScriptFullPath . """" . A_Space . params, str, A_WorkingDir, int, 1)
(btw, I hope these broken links get fixed soon... they could totally turn off a new user)

  • Members
  • 571 posts
  • Last active: Aug 01 2015 03:45 PM
  • Joined: 15 Sep 2006
Link fixed..
If i've seen further it is by standing on the shoulders of giants

my site | ~shajul | WYSIWYG BBCode Editor

  • Members
  • 1 posts
  • Last active:
  • Joined: 20 Jun 2013

RunAsAdmin() works great for me with one caveat: If I have #Warn enabled, I get a variety of warning popups related to global vs local variable and unassigned variables.


Here is diff for my fix


  RunAsAdmin() {
+   global 0   
+   param  :=
+   params :=
     Loop, %0%  ; For each parameter:
        param := %A_Index%  ; Fetch the contents of the variable whose name is contained in A_Index.
        params .= A_Space . param
with the above change I don't get any warnings.

  • Members
  • 44 posts
  • Last active: Oct 13 2014 09:43 PM
  • Joined: 11 Oct 2006
If RunAsAdmin()

MsgBox, % A_IsAdmin

RunAsAdmin() {
  IfNotEqual, A_IsAdmin, 1, Run, % "*RunAs " DllCall("GetCommandLine", "Str"),, % "UseErrorLevel"
  Return, ErrorLevel

  • Members
  • 57 posts
  • Last active: Mar 11 2019 07:48 AM
  • Joined: 23 Sep 2008

as Function, shorter code, work with both Basic AHK, Ansi and Unicode Autohotkey_L

RunAsAdmin() {
	Global 0
	IfEqual, A_IsAdmin, 1, Return 0
	Loop, %0%
		params .= A_Space . %A_Index%
	DllCall("shell32\ShellExecute" (A_IsUnicode ? "":"A"),uint,0,str,"RunAs",str,(A_IsCompiled ? A_ScriptFullPath
	: A_AhkPath),str,(A_IsCompiled ? "": """" . A_ScriptFullPath . """" . A_Space) params,str,A_WorkingDir,int,1)

  • Members
  • 48 posts
  • Last active: Feb 12 2016 11:21 PM
  • Joined: 25 Apr 2013

Hi All,


I'm having no luck with this and am hoping someone may be able to clarify how to use this.


From what I understand I need to add add the code runasadmin.ahk to a library script, and then add the function runasadmin() to the top of my script so when I 'run' a program in my script it will run it as an administrator.


When I try this I simply get the error 'Call to non-existent function'


I added  the code from runasadmin.ahk to a file called acc.ahk which I saved I have saved in the location c:\programs files\autohotkey\lib


I also tried putting runasadmin.ahk in the same folder that the script I'm working on is located in and I received the same message.


Any Idea what I'm doing wrong?


Thanks for your help

  • Members
  • 1228 posts
  • Last active: Apr 08 2017 06:17 PM
  • Joined: 29 May 2006

You can just copy and paste the function inside your script. You don't need to use the library folder.


If you want to use the library folder, then you need to paste the function into a file and save it in your library folder using the function name as the file name eg ....\Lib\RunAsAdmin.AHK