Run a Program Elevated via the Right Click Menu – Run as Administrator

By definition, an HTML Application (HTA or HTAs) is a Microsoft Windows application which source code consists of HTML and one of scripting languages supported by Internet Explorer (VBScript or Jscript). The file extension of an HTA is .hta, and it is executed using the program mshta.exe. I personally like HTA because its graphical user interface enables me to overcome some of the limitations of the command window, and makes it easier to run my scripts. Unfortunately HTA does not automatically trigger a User Account Control, it doesn’t have the “Run as administrator” in the context menu, and there is no compatibility tab where it could be configured to run as administrator.
To adjust the context menu (for an HTA file) so that you can launch the application as administrator, there is a simple registry tweak that has been tested on Windows XP, Vista, Windows 7 and Windows 8.
The following registry script (CreateContextMenuForHTA.reg) creates this context menu for HTA files:

Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\htafile]
@=”HTML Application”
[HKEY_CLASSES_ROOT\htafile\DefaultIcon]
@=”C:\\WINDOWS\\system32\\mshta.exe,1″
[HKEY_CLASSES_ROOT\htafile\Shell]
[HKEY_CLASSES_ROOT\htafile\Shell\RunAs]
[HKEY_CLASSES_ROOT\htafile\Shell\RunAs\Command]
@=”C:\\WINDOWS\\system32\\mshta.exe \”%1\” %*”

In the case that a registry tweak would not be allowed as a solution, that you can run an HTA in elevated mode by using the following VBScript (RunHtaAsAdministrator.vbs) which assumes that you have a folder C:\MyHtaScripts where you keep your HTA files.

' Filename:    RunHtaAsAdmin.vbs
' Date:        Jan. 2013
' Author:      Alex Dujakovic
' Description: Run HTA file as administrator
' ************************************************************
strHTAfolder = "C:\MyHtaScripts"

RunAsAdminSelectedHTA()
Sub RunAsAdminSelectedHTA
  Set objShell = CreateObject("Shell.Application")
  Set objFolder = objShell.Namespace(strHTAfolder)
  Set objFolderItem = objFolder.ParseName(ChecUserInput())
  objFolderItem.InvokeVerb "runas"
End Sub
Function ChecUserInput
  strHtaAppName = InputBox("Enter the name of your HTA Application", "HTA Application - Run As Administrator")
  If(strHtaAppName) <> "" Then
    Set fso = CreateObject("Scripting.FileSystemObject")
      If fso.fileExists(strHTAfolder & "\" & strHtaAppName) Then
        ChecUserInput = strHtaAppName
      Else
        MsgBox "Typed name is incorrect, please try again."
        ChecUserInput()
      End If

  ElseIf(strHtaAppName) = vbNullString Then
    MsgBox "That is an incorrect entry, please try again"
    WScript.Quit
  End If
End Function






Please note: Although the author has made every reasonable attempt to achieve complete accuracy of the content, he assumes no responsibility for errors or omissions. Also, you should use this information as you see fit, and at your own risk.

2 thoughts on “Run a Program Elevated via the Right Click Menu – Run as Administrator

  1. dieseyer

    HTA Helpomatic


    If HTAElevate() = True Then
    MsgBox “Run mmc . . .”, 4096
    CreateObject(“WScript.Shell”).Run “mmc.exe compmgmt.msc”, , True
    Call Main()
    End If

    Sub Main()
    MsgBox “HTA-Ende”, 4096
    End Sub

    ‘*** v13.3 *** http://www.dieseyer.de *****************************
    Function HTAElevate()
    ‘***********************************************************
    ‘ Unter Windows x64 laufen VBS’ nach einem Doppelklick in der x64-Umgebung
    ‘ mit %WinDi%\System32\wscript.exe oder mit %WinDi%\System32\cscript.exe.
    ‘ In der x64-Umgebung laufen VBS aber nicht (richtig). Die Prozedur
    ‘ HTAElevate() erkennt dies und startet ggf. das VBS in der

    Const Elev = ” /elevated”

    ‘ MsgBox oHTA.commandLine, , “5016 :: ”

    ‘ Trace32Log “5018 :: oHTA.commandLine: ==” & oHTA.commandLine & “==”, 1

    HTAElevate = True

    ‘ If InStr( LCase( oHTA.commandLine ), Elev) > 0 then MsgBox oHTA.commandLine, , “5022 :: ”
    If InStr( LCase( oHTA.commandLine ), Elev) > 0 then Exit Function

    On Error Resume Next
    window.resizeto 750, 10 ‘ : window.moveto screen.width / 2, screen.height / 2
    On Error GoTo 0

    ‘ MsgBox oHTA.commandLine, , “5030 :: ”

    createobject(“Shell.Application”).ShellExecute “mshta.exe”, oHTA.commandLine & Elev, “”, “runas”, 1

    HTAElevate = False

    self.close

    End Function ‘ HTAElevate()

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *