Search code examples
vbscriptelevated-privileges

How to pass arguments to a script that auto run as administrator?


How do I pass arguments to a script that auto run as administrator?

If WScript.Arguments.Count >= 1 Then
  Command1 = WScript.Arguments.Item(0)
End If

If WScript.Arguments.Named.Exists("elevated") = False Then
  CreateObject("Shell.Application").ShellExecute "wscript.exe", """" & _
    WScript.ScriptFullName & """ /elevated", "", "runas", 1
  WScript.Quit
End If

WScript.Echo Command1

Solution

  • Add the arguments to the argument list you already have (/elevated):

    If WScript.Arguments.Count >= 1 Then
      Command1 = WScript.Arguments.Item(0)
    End If
    
    If WScript.Arguments.Named.Exists("elevated") = False Then
      CreateObject("Shell.Application").ShellExecute "wscript.exe", """" & _
        WScript.ScriptFullName & """ " & Command1 & " /elevated", "", "runas", 1
      WScript.Quit
    End If
    
    WScript.Echo Command1