How to sudo on powershell on Windows

You can utilize the Start-Process command and then use parameter -Verb runas to elevate. This works great for starting an elevated process.

I created a sudo function like this and added it to my powershell profile:

function sudo {
    Start-Process @args -verb runas
}

Example: Open notepad as Admin to edit hosts file

sudo notepad C:\Windows\System32\drivers\etc\hosts

If you want to elevate a Powershell command, you can create a simple function like this:

function Start-ElevatedPS {
    param([ScriptBlock]$code)

    Start-Process -FilePath powershell.exe -Verb RunAs -ArgumentList $code
}

Then, call the function and pass command wrapped in {} (script block)

Example: Elevate to create a symbolic link

Start-ElevatedPS { New-Item -ItemType SymbolicLink -Name mySymlink.ps1 -Target C:\myTarget.ps1 }

If you are using Chocolatey (a package manager), you can install a package named sudo.
Then you can use sudo like Linux 😋 sudo


As of today (October 2021), winget install gerardog.gsudo did the trick (on windows 10 home edition). Edit: Tested on Windows 11 as well (April 2022)

after that, you can do this:

gsudo notepad C:\windows\system32\something-editable-by-admin-only.txt

To test if it's working, or in your case:

gsudo powershell.exe install.ps1

You will be prompted by windows` UAC to elevate your priveleges by gsudo, and you can read the source code here: https://github.com/gerardog/gsudo


Note: If you're looking to add general-purpose, prepackaged sudo-like functionality to PowerShell, consider the
Enter-AdminPSSession (psa) function from this Gist, discussed in the bottom section of this answer.

If you are running from PowerShell already, then use Start-Process -Verb RunAs as follows:

Start-Process -Verb RunAs powershell.exe -Args "-executionpolicy bypass -command Set-Location \`"$PWD\`"; .\install.ps1"

Note:

  • The script invariably runs in a new window.
  • Since the new window's working directory is invariably $env:windir\System32, a Set-Location call that switches to the caller's working directory ($PWD) is prepended.
    • Note that in PowerShell (Core) 7+ (pwsh.exe) this is no longer necessary, because the caller's current location is inherited.
  • Executing Set-Location necessitates the use of -Command instead of -File.
    • A general caveat is that -Command can change the way arguments passed to your script are interpreted (there are none in your case), because they are interpreted the same way they would be if you passed the arguments from within PowerShell, whereas -File treats them as literals.

If you're calling from outside of PowerShell, typically from cmd.exe/ a batch file, you need to wrap the above in an outer call to powershell.exe, which complicates things in terms of quoting, unfortunately:

powershell.exe -command "Start-Process -Verb RunAs powershell.exe -Args '-executionpolicy bypass -command', \"Set-Location `\"$PWD`\"; .\install.ps1\""

Interactively, of course, you can:

  • Right-click the PowerShell shortcut (in your taskbar or Start Menu, or on your Desktop), select Run as Administrator to open a PowerShell window that runs with admin privileges, and run .\install.ps1 from there.

  • Alternatively, from an existing PowerShell window, you can open a run-as-admin window with Start-Process -Verb RunAs powershell.exe, as in AdminOfThings' answer.