Running script on elevated cmd.exe

1.9k Views Asked by At

How do I ran a script after I open a command window using the below script?

Set objSh = CreateObject("Shell.Application")
objSh.ShellExecute "cmd.exe", "uac" , "", "runas", 1

For example, how do I run ipconfig as an admin using the above script?

1

There are 1 best solutions below

3
On

If you specify the /c switch, then cmd.exe will carry out the specified command and then terminate.

So, for example:

Set objSh = CreateObject("Shell.Application")
objSh.ShellExecute "cmd.exe /c ipconfig", "uac" , "", "runas", 1

Alternatively, you could use the /k switch, which works exactly the same way, except it keeps the command prompt on the screen once your command finishes executing.