Using sudo in PowerShell (Elevating your commands)

Lasse D. Skaalum
2 min readApr 10, 2022
Example of using the sudo command in PowerShell (from Windows Terminal)

If you are tired of clicking the start menu icon, right clicking PowerShell and picking “Run as administrator” just to remove a protected file or install a new program from command line, then you have come to the right place!

Basically all you need to do is paste the code below into your PowerShell window (or put it in your PowerShell profile):

Sudo as a PowerShell function

Then you will be able to run admin commands simply by typing sudo <some-admin-command> . Pretty neat, right?

Now, what is actually happening?

  • Start-Process powershell.exe opens a new PowerShell window.
  • -Verb RunAs tells the new PowerShell window to run as administrator.
  • -Args specifies the parameter(s) which are going to be used for the new PowerShell window.
  • -ExecutionPolicy Bypass means that we are able to execute any commands without warnings or prompts.
  • -Command specifies the commands we are going to run.
  • Set-Location '@pwd' will change your directory to the directory, which you originally used your command from. This is necessary if you want to e.g. remove a file from the directory, which you are currently in.
  • ; is Powershells way of executing commands in sequence (equivalent to doing && in bash).
  • $args represents all the arguments, which we type into PowerShell after sudo.

There you go! I hope you found this article useful. If you did please leave a 👏

--

--

Lasse D. Skaalum

Hi, my name is Lasse and I’m a software engineer from Denmark.