run cmd.exe as administrator in a script

Robert picture Robert · Jul 12, 2012 · Viewed 29.7k times · Source

I have a script that I need to run as Administrator just as I would right click on cmd.exe and click Run As Administrator.

Currently this is what I have:

Call WSHShell.Run("cmd.exe /K netdom renamecomputer ... end code")

Answer

Jon Swanson picture Jon Swanson · Jul 22, 2012

Have you tried using ShellExecute?

RunCmdElevated.vbs

Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "cmd.exe", "/k echo test", "", "runas", 1

wscript RunCmdElevated.vbs

gives

test

C:\Windows\system32>

in a new window. The "1" is the view mode

http://ss64.com/vb/shellexecute.html