I always work on a non-administrator account on my Windows computer. Sometimes I need to install programs which requires administrator access. As I mostly use the Windows command prompt, is there a Windows command to escalate privileges, similar to the Linux terminal command sudo
?
19 Answers
The runas command.
runas [{/profile|/noprofile}] [/env] [/netonly] [/smartcard] [/showtrustlevels] [/trustlevel] /user:UserAccountName program
Just run:
runas /noprofile /user:Administrator cmd
to start a command shell as a administrator
-
23You might find you want the profile loaded (e.g. including environment variables) for any extended use. In which case drop the
/noprofile
.– RichardCommented Sep 17, 2009 at 9:38 -
6This is not working for me. After I typed my password the command prompt is closed.– JonasCommented Dec 9, 2010 at 13:44
-
64isn't this asking for the Administrator's password? sudo is asking for your password!– n611x007Commented Sep 18, 2012 at 8:42
-
21Unfortunately, this is seriously outdated.
Runas
merely runs commands under a different set of credentials. Even though your credentials have admin permissions, it doesn't mean all processes under your credentials run as admin.– surfasbCommented Jan 25, 2014 at 0:59 -
19+1 for "outdated" since runas can't bypass UAC. Instead, press Windows button, type cmd, and press Ctrl+Shift+Enter. Commented Jan 22, 2016 at 12:06
Elevate - "executes a command with UAC privilege elevation. This is useful for working inside command prompts or with batch files." It's not the same as sudo
, it changes the executing user to Administrator, but its syntax is a lot more straightforward to use than runas
, and it can keep the current directory, enabling the use of relative paths.
Synopsis:
elevate [(-c | -k) [-n] [-u]] [-w] command
Options:
-c Launches a terminating command processor; equivalent to "cmd /c command".
-k Launches a persistent command processor; equivalent to "cmd /k command".
-n When using -c or -k, do not pushd the current directory before execution.
-u When using -c or -k, use Unicode; equivalent to "cmd /u".
-w Waits for termination; equivalent to "start /wait command".
Elevate's purpose isn't to work around or bypass UAC (User Account Control), but to work with it. As long as UAC is enabled there has to be some kind of prompt at some point in the process. If you need to get rid of prompting altogether you have to disable UAC.
The pain point Elevate alleviates is escalating a particular process from a non-privileged shell and then carrying on as normal. Without this you need to start a privileged command prompt with right-click > "Run as Administrator" before attempting the privileged command, which can't be easily scripted.
This works well with "Elevate without prompting" in secpol.msc
. Together, they do the same as %wheel ALL=(ALL) NOPASSWD: ALL
in sudo
A known limitation is that it does not return the error code from the program it is elevating.
If your muscle memory is stuck on sudo, create an alias using Doskey:
doskey sudo=elevate -w
or batchfile in PATH:
@elevate -w %*
Elevate is 3rd party tool written by Johannes Passing. It's an 11kb download and portable (no install needed): http://code.kliu.org/misc/elevate/
-
6This goes perfect with "Elevate without prompting" in secpol.msc. Together, they do the same as
%wheel ALL=(ALL) NOPASSWD: ALL
in sudo.– sayapCommented Feb 20, 2012 at 9:40 -
8The only trouble I find with elevate (v1.3.0) is that it does not return the error code from the program it is elevating.– KenCommented Aug 14, 2013 at 18:41
-
4
-
3@barlop: elevate's purpose isn't to work around or bypass UAC (User Account Control), but to work with it. As long as UAC is enabled there has to be some kind of prompt at some point in the process. If you need to get rid of prompting altogether you have to disable UAC (see 2nd comment). The problem elevate solves is escalating a particular process from a non-privileged shell, and then carrying on as normal. Without this you need to start a privileged command prompt with right-click "Run as Administrator", which can't be scripted, before attempting the privileged command. Commented Mar 19, 2014 at 22:40
-
17Would be nice to mention that it's an external utility, missing OOTB.– Hi-AngelCommented Dec 27, 2016 at 16:18
You can use the runas command which is kind of similar, or you can check out the sudo for Windows project over at SourceForge which adds a sudo command.
The difference is subtle:
Let's say you have two users. Bob is a normal user and James is an administrator.
If you log in as Bob and use "runas james acommand" the command is run as if it was run by James, so it accesses James' user settings and any user changes go into James My Documents & settings folders, etc. So if you are installing an application, say, it will be installed as James, not as Bob.
If on the other hand Bob does "sudo acommand" the command is still run as Bob, but with elevated permissions - just like the Linux sudo command. To prevent any user from being able to sudo you have to define a sudoers user group that contains the list of the normal users that have permission to elevate using sudo. The users still have to provide credentials before elevation.
Sometimes the difference isn't important, sometimes it is, and I find that both commands can be useful.
-
2Are you sure? When I run sudo in ubuntu, if my current theme is in
~/.themes
then the sudo-ed application will not be able to access that theme, because it's not in/home/root/.themes
, and will use the default ugly gtk theme.– hasenCommented Nov 19, 2009 at 22:28 -
5@hasen j - your issue is just because ~/.themes evaluates before the command is run (and thus before it switches over to root).– JaredCommented Dec 15, 2011 at 18:52
-
2Another solution, except hosted on GitHub, is windosu: github.com/tehsenaus/windosu I just found it, and it seems to work great. Favorite thing is it's super easy to install. Just "npm install -g windosu".– VenryxCommented Jun 6, 2017 at 7:52
-
This is elevation of a user account in Administrators; sudo is another thing.– MassimoCommented Jan 13, 2020 at 10:12
-
You can also use the Script Elevation PowerToys.
-
Awesome - exactly what I was looking for. I didn't want to run as a command has a different user, only to run it with elevated privileges.– oripCommented Mar 14, 2010 at 12:55
-
Exactly the right solution for this problem! Who wants Runas with its messy syntax? Commented Mar 29, 2010 at 14:14
-
1I've often wished the elevate command were built into windows. It's a fantastic tool.– nhinkleCommented Nov 11, 2010 at 23:43
-
there's now an Elevation PowerToys collection by the same author from which Creating a Self-Elevating Script is particularly relevant. Commented Sep 7, 2011 at 23:22
-
1@barlop of course not. That would defeat the purpose of UAC.– nhinkleCommented Mar 15, 2014 at 17:07
If you are ready to switch to alternative consoles, there is ConEmu (I'm the author). One of its features - the ability to run both elevated and non-elevated tabs in the one ConEmu window. Tabs may be started with different credentials too.
For user comfort, there is batch-file csudo.cmd (which may be easily adopted to bash). Read full description in project's wiki. In brief, when you run some command from existing non-elevated tab, for example
csudo dism /online /enable-feature /featurename:NetFX3 /All /Source:D:\sources\sxs /LimitAccess
ConEmu will starts dism
in the new elevated console/tab (with preceding UAC prompt in Vista or Login box in XP).
By default csudo
starts new console in a split (may be changes via editing of csudo.cmd
contents).
And of course you may rename it to sudo.cmd
if you like "classic" sudo
word.
-
1csudo long ago was included in the ConEmu distribution. No link is needed at all.– MaximusCommented Mar 12, 2014 at 8:16
-
problem here is the new console tab becomes unusable after the sudo'd command. So you can't continue working in the elevated tab, just get a
press enter or esc to exit
message. Commented Jan 17, 2016 at 15:25 -
1Of course you cant. Even on Unix you are back to non-elevated terminal after
sudo
command is done its work! You are going wrong way.– MaximusCommented Jan 17, 2016 at 17:45 -
11) if you're gonna open a new window or tab you might as well keep it, let the user choose if they want to continue. 2) On my ubuntu I don't have to sudo every time, elevation lasts a few minutes I think. 3) this application has a million options, yet nothing for this? Maybe I'm spoiled. Commented Jan 18, 2016 at 2:31
Quick method:
Three steps to add sudo.
Open PowerShell.
Copy the following script (Ctrl+C) and paste it in PowerShell (Alt+Space+E+P):
$script_path="$HOME\Documents\Scripts"; if (!(test-path $script_path)) {New-Item -ItemType directory $script_path} if (!(test-path $profile)) { new-item -path $profile -itemtype file -force }". $script_path\sudo.ps1" | Out-File $profile -append; "function sudo(){if (`$args.Length -eq 1){start-process `$args[0] -verb `"runAs`"} if (`$args.Length -gt 1){start-process `$args[0] -ArgumentList `$args[1..`$args.Length] -verb `"runAs`"}}" | Out-File $script_path\sudo.ps1; powershell
- Hit Enter.
It will permanently enable sudo
command in PowerShell.
Usage:
sudo <process-name> [param1 [param2 [param3]]]
Examples:
sudo explorer
sudo notepad
sudo powershell
sudo cmd
sudo taskmgr
sudo tasklist
sudo taskkill /IM Skype.exe /PID 8496
Long method for learning:
- Read this article.
- Read the comments.
- Take a look at Stephen's git repository and the readme file.
Note: I mixed the script from both articles to create the aforementioned script. Rather manually pasting the script in notepad I added the Out-File
statements to save ps1
and $profile
files from the script.
Tip: If you are not a very big fan of UAC popups (like me), save the following in *.reg file and run it:
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System]
"ConsentPromptBehaviorAdmin"=dword:00000000
-
1I love the powershell solution for this question. This has to be the easiest way to elevate something. Existing .bat files can easily be converted to POSH Commented Feb 14, 2013 at 18:05
-
Got this error on windows 10:
. : File C:\Users\User\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 cannot be loaded because running scripts is disabled on this system.
– sowrovCommented Nov 19, 2015 at 9:51 -
@sowrov, see [this article](www.faqforge.com/windows/windows-powershell-running-scripts-is-disabled-on-this-system/). By default PowerShell scripts are disabled on any Windows installation. You need to enable it with this command
set-executionpolicy remotesigned
. Other options besidesremotesigned
are described in official docs. Commented Nov 22, 2015 at 9:54 -
Readers note this ps script is a wrapper for the
powershell -c start -verb runas program.exe
functionality. Also note you can't elevate like this with the cmd prompt (natively). Powershell is the best native option. Commented Jan 17, 2016 at 13:58 -
can the same be done in cmd.exe instead of powershell env? powershell does not compute with clink and without clink windows does not compute at all...– PJJCommented Feb 26, 2019 at 7:39
If you're doing this on Windows, then in addition to the Run As command as mentioned in a couple of other answers, there are also ways to do this with the mouse.
If you hold down the Shift key as you right-click on most executable files in Windows you should notice a few more advanced options. One of these is the "Run As...
" option (I think it's called "Run As Administrator
" from Vista onwards).
You can also download a more advanced version of RunAs from Microsoft, called ShellRunAs, this has enhancements over the built-in RunAs command, both in command line and graphical modes, including letting you save account credentials
-
This is elevation of a user account in Administrators; sudo is another thing.– MassimoCommented Jan 13, 2020 at 10:11
-
The question was about elevating with command! F.e. from a batch file, when detected that certain operation requires elevation. Commented Mar 24, 2020 at 17:26
-
@AnrDaemon I'm surprised to be admonished about the relevance of an answer that I left here a decade ago!– GAThrawnCommented Mar 30, 2020 at 16:23
I wrote gsudo, a sudo for windows that feels like *nix sudo
and has a few killer features:
- Run within the current console (attached) without breaking tab-key auto-complete. Or add
-n
to launch in a new window. - Handles all scenarios reliably to be used on scripts. (ExitCodes, StdIn/Out/Err Redirection/Capture)
- Supports Cmd/PowerShell/PowerShell Core
- Credentials cache: If
gsudo
is invoked several times within minutes it only shows the UAC pop-up once.
Usage
gsudo
Opens an elevated shell in the current console.
gsudo [options] {command} [arguments]
Executes the specified command with elevated permissions.
Most relevant [options]
:
-n | --new
Starts the command in a new console with elevated rights (and returns immediately).-w | --wait
Force wait for the process to end (and return the exitcode).-s | --system
Run As Local System account ("NT AUTHORITY\SYSTEM").
Installation
- Using Scoop:
scoop install gsudo
- Using Chocolatey:
choco install gsudo
- Using Winget:
winget install gsudo
- Or check the docs and latest release
-
1This looks great, but does it rely on the UAC graphical prompt? I am logging in via SSH and wants something that works purely in a command line mode. Commented Mar 30, 2020 at 12:09
-
1
Surun is free, open-source application that allows certain programs to run with administrative rights, without providing a password without changing the user registry or modify environment variables.
When I was using Windows XP this app helps me a lot. Beta works under Windows 7.
-
surun does a great job. However sometimes under Windows 8 and 8.1 its defaults aren't ideal. Because it replaces Windows's run as function, you are not able to start a command prompt with right click on the start screen.– WolfCommented Jan 15, 2015 at 11:15
-
This is the best answer and should be the accepted one. Most of the other answers are about elevation of a user account in Administrators: not sudo.– MassimoCommented Jan 13, 2020 at 10:13
The simplest solution in my view is to leverage powershell to do the work, which is portable and will prompt the user using the UAC.
You can just run this in any shell (cmd or powershell)
powershell Start-Process -verb runAs path-to-your.exe "-all -args -in -quotes"
There is a chocolatey package for it with the convenient name sudo. You can install the package with chocolatey using this command:
choco install -y sudo
Then in whatever Windows/MS shell you have to use, you can use sudo
as expected.
-
This is elevation of a user account in Administrators; sudo is another thing.– MassimoCommented Jan 13, 2020 at 10:11
As you've probably discovered, runas
will let you run as another user but it cannot do elevation and it doesn't pass current directories, environment variables or long command lines.
Hamilton C shell solves that with a genuine su
and sudo
. su
lets you run a command as another user; sudo
(actually an alias to su
) lets you run a command elevated. You can also do both, running elevated as a different user. Current directories, environment variables and long command lines are passed by way of a shared memory handshake between su
running in the caller's context and a copy of itself running as an interlude with the new credentials that then starts the child. Full disclosure: I'm the author.
There is now an official Sudo for Windows. It allows users to run elevated commands directly from unelevated terminal windows.
Sudo is available for Windows 11 builds 26045 and later. If you're on an Insiders build with sudo, you can enable it in the Windows Settings app, on the "Developer Features" page.
A while ago I created wsudo
, an open-source sudo
-like CLI tool for Windows to run programs or commands with elevated rights, in the context of the current directory. It's freely available as a Chocolatey package.
I use it a lot for things like configuring build agents, admin stuff like sfc /scannow
, dism /online /cleanup-image /restorehealth
or simply for installing/updating my local Chocolatey packages (e.g., wasudo cup all -y
). Use at your own risk.
Installation
choco install wsudo
Chocolatey must be already installed.
Purpose
wsudo
is a Linux sudo
-like tool for Windows to invoke a program with elevated rights (as Administrator) from a non-admin shell command prompt and keeping its current directory.
This implementation doesn't depend on the legacy Windows Script Host (CScript
). Instead, it uses a helper PowerShell 5.1 script that invokes "Start-Process -Wait -Verb runAs ..."
cmdlet. Your system most likely already has PowerShell 5.x installed, otherwise you'll be offered to install it as a dependency.
Usage
wsudo
runs a program or an inline command with elevated rights in the current directory. Examples:
wsudo .\myAdminScript.bat
wsudox "del C:\Windows\Temp\*.* && pause"
wasudo cup all -y
wasudox start notepad C:\Windows\System32\drivers\etc\hosts
I often invoke it from the Windows Run box (Win+R), or by typing wasudo
Enter in the Windows Explorer address bar (Alt+D). The latter opens an admin command prompt in the Explorer's current folder.
For more details, visit the GitHub repro.
A working sudo
replacement for Cygwin's mintty
terminal would be to place the following script in user's PATH:
$!/bin/bash
cygstart --action=runas mintty -e `which bash` -lc \"$@\"
For me this is the only viable replacement to elevate privileges of programs like vim
or cygrunsrv
while working in a terminal on Windows.
This script does the job:
@echo Set objShell = CreateObject("Shell.Application") > %temp%\sudo.tmp.vbs
@echo args = Right("%*", (Len("%*") - Len("%1"))) >> %temp%\sudo.tmp.vbs
@echo objShell.ShellExecute "%1", args, "", "runas" >> %temp%\sudo.tmp.vbs
@cscript //NoLogo %temp%\sudo.tmp.vbs
Save it as sudo.cmd
then add it to your PATH
Note: the runas
means in this context "Run as administrator" and not "Run as other user"
Taken from here and slightly edited to remove cscript.exe header from output
-
1This creates a new console. The new window will disappear after the sudo'd command is completed--so you can't even read output. Commented Jan 17, 2016 at 14:09
-
Sadly, yes. But it can be used for some basic commands (copy), GUI tools, or opening an admin cmd.– SylveonCommented Jan 17, 2016 at 18:04
-
This is elevation of a user account in Administrators; sudo is another thing.– MassimoCommented Jan 13, 2020 at 10:10
The following vbs script allows to launch a given command with arguments with elevation and mimics the behavior of the original unix sudo
command for a limited set of used cases (it will not cache credentials nor it allows to truly execute commands with different credentials). I put it on C:\Windows\System32
.
Set objArgs = WScript.Arguments
exe = objArgs(0)
args = ""
IF objArgs.Count >= 2 Then
args = args & objArgs(1)
End If
For it = 2 to objArgs.Count - 1
args = args & " " & objArgs(it)
Next
Set objShell = CreateObject( "WScript.Shell")
windir=objShell.ExpandEnvironmentStrings("%WINDIR%")
Set objShellApp = CreateObject("Shell.Application")
objShellApp.ShellExecute exe, args, "", "runas", 1
set objShellApp = nothing
Example use on a command prompt sudo net start service
-
Downvoters: еxplain why and what is not working for you. The script is tested and working for me.– ceztkoCommented Nov 6, 2019 at 19:56
-
This is elevation of a user account in Administrators; sudo is another thing.– MassimoCommented Jan 13, 2020 at 10:11
-
@Massimo of course calling this script
sudo
is a small naming abuse: it doesn't and it will not have the same features of originalsudo
, but by default in unix will execute with super user (root) impersonification, providing this is allowed insudoers
, so the mimic of the provided example it's perfectly clear to me. It's not possible to create a fullsudo
replacement in Windows without access to Windows internals and without defining a clear semantic of what can and what can't be done by the command. If this quick and dirty script doesn't work for you should find a different solution– ceztkoCommented Jan 13, 2020 at 10:34
Another option is TakeCommand's START /ELEVATED …
switch.
The noninteractive (TCC-RT) version of the suite is offered for free. The "sudo.btm" batch file would look like
@START "" /ELEVATED %*
which would also support the form sudo /wait <someprogram> /params
The scoop creator has a simple CLI that works great.
Install with: scoop install sudo
As expected, you need to click UAC dialog to elevate the new process.
$ sudo mkdir 'C:\Program Files\whatever'
Directory: C:\Program Files
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 3/11/2024 2:47 PM whatever
doskey sudo= runas /user:Administrator "cmd /k cd \"%cd%\" & $*"