简体   繁体   中英

Run elevated powershell command from command prompt

powershell.exe Start-Process powershell.exe "set-executionpolicy" -Verb runAs

The above command will run a powershell window as admin and execute the command before it, however, if I want to make it say set-executionpolicy bypass , it will error out. How can I pass this parameter through the top command?

Below is the error I get when I pass said parameter:

    Start-Process : A positional parameter cannot be found that accepts argument
'bypass'.
At line:1 char:1
+ Start-Process powershell.exe set-executionpolicy bypass -Verb runAs
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Start-Process], ParameterB
   indingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell
   .Commands.StartProcessCommand

您可以直接在PowerShell.exe上使用–ExecutionPolicy开关来绕过执行策略。

powershell.exe –ExecutionPolicy Bypass

Start-Process accepts an array of arguments in order to pass into created process. So, in order to launch an elevated Powershell that would execute a Set-ExecutionPolicy cmdlet you need to wrap the arguments into an array.

start-process -filepath powershell.exe -argumentlist @('-command','Set-ExecutionPolicy Bypass') -verb runas

As a side note, you probably want to set execution policy with -scope argument for it to actually be effective once that process ends.

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