run CMD as administrator in PowerShell

Roei Givati picture Roei Givati · Nov 11, 2018 · Viewed 14.9k times · Source

I'm trying to execute a command prompt as administrator by using powershell. (like when you press right click on cmd icon and choose run as administrator). what should I add to the following in order to do so?

& cmd.exe /c $VAR

Answer

mklement0 picture mklement0 · Nov 11, 2018

Somewhat obscurely, you must use Start-Process with argument -Verb RunAs in order to launch an elevated process (a process with administrative privileges) in PowerShell:

# The command to pass to cmd.exe /c
$var = 'echo hello world & pause'

# Start the process asynchronously, in a new window,
# as the current user with elevation (administrative rights).
# Note the need to pass the arguments to cmd.exe as an *array*.
Start-Process -Verb RunAs cmd.exe -Args '/c', $var