how to execute set of commands in elevated mode of powershell

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



4
down vote favorite
1
share [g+] share [fb] share [tw]

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

active oldest votes
up vote 4 down vote

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

你可能感兴趣的:(command,File,exe,menu,powershell)