How to Launch a Vbscript in Elevated Mode (Run as Administrator)

With the introduction of User Account Control (UAC) in Windows Vista, you usually open an elevated Command Prompt in order to run batch files and scripts that need administrative privileges. Applications can make use of manifest files (using the RequireAdministrator flag) to automatically run elevated.

For scripts, this article provides you some neat little tricks using which you can automatically elevate using the ShellExecute “runas” parameter.

(See also How to Automatically Elevate a Batch file to Run it as Administrator? for another auto-elevation method.)

Launch a Vbscript elevated

Thanks to Jim Barry for tipping me off about the using runas argument in the ShellExecute method of Shell.Application object. Using Jim’s suggestions, my original script was condensed down to a great deal. Use one of these methods to run VBScripts elevated.

Method 1

Here is a sample script that re-launches itself as administrator (elevated) using the runas parameter, if the script has no command-line arguments passed. When re-launching the script as administrator, simply pass a bogus argument so that the script does not run in a cyclic loop.

If WScript.Arguments.length = 0 Then
   Set objShell = CreateObject("Shell.Application")
   'Pass a bogus argument, say [ uac]
   objShell.ShellExecute "wscript.exe", Chr(34) & _
      WScript.ScriptFullName & Chr(34) & " uac", "", "runas", 1
Else
   'Add your code here

End If

Method 2

This method uses a launcher script which runs the main VBScript elevated using the runas verb.



Set objShell = CreateObject("Shell.Application")
Set FSO = CreateObject("Scripting.FileSystemObject")
strPath = FSO.GetParentFolderName (WScript.ScriptFullName)
If FSO.FileExists(strPath & "\MAIN.VBS") Then
     objShell.ShellExecute "wscript.exe", _
        Chr(34) & strPath & "\MAIN.VBS" & Chr(34), "", "runas", 1
Else
     MsgBox "Script file MAIN.VBS not found"
End If

You’ll see see the UAC elevation dialog.

automatically elevate vbscript runas

Once user clicks Continue to approve, the main script is launched as administrator.

RELATED: How to Automatically Elevate a Batch file to Run it as Administrator?


One small request: If you liked this post, please share this?

One "tiny" share from you would seriously help a lot with the growth of this blog. Some great suggestions:
  • Pin it!
  • Share it to your favorite blog + Facebook, Reddit
  • Tweet it!
So thank you so much for your support. It won't take more than 10 seconds of your time. The share buttons are right below. :)

Ramesh Srinivasan is passionate about Microsoft technologies and he has been a consecutive ten-time recipient of the Microsoft Most Valuable Professional award in the Windows Shell/Desktop Experience category, from 2003 to 2012. He loves to troubleshoot and write about Windows. Ramesh founded Winhelponline.com in 2005.

2 thoughts on “How to Launch a Vbscript in Elevated Mode (Run as Administrator)”

  1. Found this useful method after reading about it on TenForums – but your page doesn’t display fully on Firefox (78.0.1) even after disabling tracking protection (all ad boxes show spinning circles). It works fine in ‘new’ Edge.

    Reply

Leave a Comment