Skip to content

Instantly share code, notes, and snippets.

@codeartery
Created January 6, 2021 16:23
Show Gist options
  • Save codeartery/f9fa041933773628ddc9e60b67dfef20 to your computer and use it in GitHub Desktop.
Save codeartery/f9fa041933773628ddc9e60b67dfef20 to your computer and use it in GitHub Desktop.
Will run the current VBScript as an administrator.
REM:<RunAsAdmin>
' Place this code at the top of your VBScript file to run the script as an admin.
' It will quit the current script and re-run the script as an administrator.
' Note that depending on your UAC permissions you may be prompted.
' Does not support command line arguments.
If Not WScript.Arguments.Named.Exists("AsAdmin") Then
CreateObject("Shell.Application").ShellExecute "WScript.exe", """" & WScript.ScriptFullName & """ /AsAdmin", "", "runas", 1
WScript.Quit
End If
REM:</RunAsAdmin>
' Your code here...
REM:<RunAsAdmin>
If Not WScript.Arguments.Named.Exists("AsAdmin") Then
CreateObject("Shell.Application").ShellExecute "WScript.exe", """" & WScript.ScriptFullName & """ /AsAdmin", "", "runas", 1
WScript.Quit
End If
REM:</RunAsAdmin>
' Example code that needs admin permissions to run.
Dim oWss: Set oWss = CreateObject("WScript.Shell")
Call oWss.Run("cmd /k net user administrator /active:yes")
' Call oWss.Run("cmd /k net user administrator /active:no")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment