1

If you want to run a command as admin on PowerShell on Windows, you can write something like this:

Start-Process foo -Verb RunAs -ArgumentList '...'

But it is not possible on Linux. Powershell will return you the following error: Start-Process: The parameter '-Verb' is not supported for the cmdlet 'Start-Process' on this edition of PowerShell. Indeed, According to the Powershell online documentation, "the [-Verb] parameter does not apply for non-Windows systems", so it is not possible to run Powershell as admin like this on Linux.

My question is simple: how to elevate Powershell privileges on Linux? How can I simulate BASH's sudo ... with Powershell on Linux? I know that you can make a workaround with bash and some thing like sudo pwsh -Command '...', but I am looking for a "100% PowerShell" way to do this.

Thanks in advance for your responses.

PS: By "PowerShell", I mean "PowerShell 7", the latest one.

air-dex
  • 4,130
  • 2
  • 25
  • 25
  • Why do you need a "100% PowerShell" way? – Bill_Stewart Feb 16 '21 at 01:00
  • @Bill_Stewart I am a Powershell user on Linux and I would like to know how to do. PowerShell is a bright CLI and I would like to get rid of Bash on my computer as much as possible. – air-dex Feb 16 '21 at 15:07
  • 2
    Isn't sudo just a standard executable on Linux (i.e., nothing specifically to do with bash)? Why not just use it? – Bill_Stewart Feb 16 '21 at 15:38

2 Answers2

2

There is no powershell-ish way of doing this, because elevation feature depends on OS. In both Windows and Linux you actually start a new process under a different security context (and under separate user session), but in Windows there is built-in elevation mechanism using verbs. Linux does not support verbs, and you have to use sudo or su.

If you go little bit deeper, Start-Process -Verb under Windows creates appropriate [System.Diagnostics.ProcessStartInfo] with .Verb filled and runs process using [System.Diagnostics.Process]::Start($ProcessStartInfo).

So, under Linux you internally have the same except instead of setting .Verb, you set FileName to sudo and move old value to arguments.

Like there is no any universal method to drive both bicycle and spaceship.

filimonic
  • 3,988
  • 2
  • 19
  • 26
1

Use sudo pwsh -c:

PS> (sudo pwsh { gci -r /opt/tomcat/*.sh | ? name -like 'startup*' }).fullname
/opt/tomcat/bin/startup.sh
phuclv
  • 37,963
  • 15
  • 156
  • 475
Ryan
  • 11
  • 1