how to execute set of commands in elevated mode of powershell

文章详细介绍了如何在PowerShell中通过命令行或脚本执行一组命令,并保持在管理员模式下操作,包括使用右键快捷方式运行、在启动菜单中输入命令并按Ctrl+Shift+Enter等方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://stackoverflow.com/questions/7681920/how-to-execute-set-of-commands-in-elevated-mode-of-powershell


i have tried the below way to execute the commands in administrator mode.

  1. 'PS>start-process powershell -verb runas $app = Get-AppxPackage -all| Where-Object{$_.Name-like "$ReleaseName"} '

    'PS>start-process powershell -verb runas Remove-AppxPackage $app.PackageFullName '

    • for the first call, it opens and executes the command successfully and closes the admin powershell instance. for the second call it requires $app information which is not available because it again opens a new PS admin window
    • i can't execute "Get-AppxPackage -all" in normal mode "-all" requires admin mode only
  2. ' PS>start-process powershell -verb runas {

$app = Get-AppxPackage | Where-Object{$_.Name-like "$ReleaseName"};

Remove-AppxPackage $app.PackageFullName

}'

tried this way but no luck.

can someone suggest me how to execute set of instructions like above in powershell elevated mode?

thanks in advance

link | improve this question

14% accept rate
 
feedback

1 Answer

The obvious way:

Open Powershell console in "elevated mode" -> Right click shortcut / exe and click Run as Administrator. Or in start menu, type Powershell and hit CTRL + SHIFT + ENTER

Then run the commands from this.

Or you can have the commands in a script (.ps1 file) and invoke that script:

start-process powershell -verb runas -argument script.ps1

I would also like to mention that in yout commands, you don't have to store it in $app, you can use something like:

Get-AppxPackage -all| Where-Object{$_.Name-like "$ReleaseName"} | Remove-AppxPackage
link | improve this answer

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值