Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to execute set of commands in elevated mode of powershell

Tags:

powershell

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

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

tried the below but no luck.

PS>start-process powershell -verb runas 
{

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

Remove-AppxPackage $app.PackageFullName

}

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

thanks in advance

like image 278
Praveen Jakkaraju Avatar asked Oct 07 '11 00:10

Praveen Jakkaraju


People also ask

How do you execute a set of commands in PowerShell?

If you want to use the output of one command as the input of another command then you can use the pipe(|) operator.It is used extensively in Windows Powershell. ie. The output of the first command acts as input to the second command and the output of the second to another command is connected via a pipe.

How do I elevate PowerShell from command line?

To do so, type or paste powershell start-process powershell -verb runas into Command Prompt, and then hit Enter. A new elevated PowerShell window will appear.

How do I Run a script in elevated Command Prompt?

Press Ctrl + Shift and double-click a shortcut to run as an elevated process.


1 Answers

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
like image 124
manojlds Avatar answered Sep 28 '22 13:09

manojlds