Search code examples
vbaexcelshellvbscriptadministrator

VBA: How to Open command prompt as Administrator and call a VBS using cscript?


I'm trying to open a command prompt as Administrator AND run a .VBS file using CScript.

I found post for running cmd as Administrator:

Shell "powershell.exe -Command " & Chr(34) & "Start-Process cmd -Verb RunAs", vbNormalFocus

Also Found a post for running a VBS file:

SFilename = "Cscript " & Chr(34) & "C:\Temp\Run.vbs " & Chr(34) & " " & pParam1 & " " & pParam2

Shell SFilename, vbNormalFocus

However, Can someone help me to get both things done in single cmd window?

I tried merging both Shell statments and running one after the other but no luck.


Solution

  • Copying @Noodles answer in order to mark this question as answered:

    Set oShell = CreateObject("Shell.Application")
    oShell.ShellExecute "cscript.exe", "//nologo c:temp\run.vbs", , "runas", 1 
    

    or for cmd

    oShell.ShellExecute "cmd.exe", "/k cscript //nologo c:temp\run.vbs", , "runas", 1