Run VBScript as admin: Difference between revisions
Jump to navigation
Jump to search
computernewb>Judyhegao m (fixed the ugly ass syntax highlighting) |
No edit summary |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
In Windows Vista, 7, 8 and |
In Windows Vista, 7, 8, 10 and 11, you may want to run a '''VBScript as admin'''. While this is possible by right clicking and hitting "Administrator" most will probably want the UAC prompt to appear, like in a program. The below code will try to elevate privileges if it is on Vista and up (but will not if running on XP, 2000, etc.) |
||
<syntaxhighlight lang="vbscript"> |
<syntaxhighlight lang="vbscript"> |
Latest revision as of 06:15, 20 May 2022
In Windows Vista, 7, 8, 10 and 11, you may want to run a VBScript as admin. While this is possible by right clicking and hitting "Administrator" most will probably want the UAC prompt to appear, like in a program. The below code will try to elevate privileges if it is on Vista and up (but will not if running on XP, 2000, etc.)
Option Explicit
Dim WMI, OS, Value, Shell
do while WScript.Arguments.Count = 0 and WScript.Version >= 5.7
'##### check windows version
Set WMI = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
Set OS = WMI.ExecQuery("SELECT *FROM Win32_OperatingSystem")
For Each Value in OS
if left(Value.Version, 3) < 6.0 then exit do
Next
'##### execute as admin privileges
Set Shell = CreateObject("Shell.Application")
Shell.ShellExecute "wscript.exe", """" & WScript.ScriptFullName & """ uac", "", "runas"
WScript.Quit
loop
'##### execute code
WScript.Echo "Hello World"