简体   繁体   中英

How I can run CMD commands as administrator with powershell?

I want to run CMD commands as admin with Powershell. There is full command list:

Dism /OnLine /CleanUp-Image /CheckHealth&Dism /OnLine /CleanUp-Image /RestoreHealth

Can be any way to do this? I want to do with Run command.

Step 1: Open the Command Prompt, and type the PowerShell as a command, then press Enter key. Step 2: Now, the command prompt will turn to Windows PowerShell. Step 3: Type the command start-process PowerShell -verb runas and press "enter" key. Step 4: It will bring up an elevated Windows PowerShell as an administrator.

Is that what you needed?

You can run most cmd commands within PowerShell without having to open another process, but if you want to elevate you will need to invoke Start-Process -RunAs to elevate the child process. For example:

$spArgs = @{
  Verb = 'RunAs';
  Path = "${env:SystemRoot}\system32\dism.exe";
  ArgumentList = "/OnLine /CleanUp-Image /CheckHealth&Dism /OnLine /CleanUp-Image /RestoreHealth";
}
Start-Process @spArgs

If you want to run a cmd built-in command however, there's no external command you can run from PowerShell (for example, mklink ). In this case you will need to run cmd.exe elevated powershell. To use the mklink example:

$spArgs = @{
  Verb = 'RunAs';
  Path = "${env:SystemRoot}\system32\cmd.exe";
  ArgumentList = "/c mklink /D LINK_PATH TARGET_PATH;
}
Start-Process @spArgs

In either scenario, if you want to run something elevated from something other than PowerShell, the commands above can be used though you will need to first proxy the call through powershell.exe -Command "Start-Process...."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM