Home » Questions » Computers [ Ask a new question ]

Is there any 'sudo' command for Windows?

Is there any 'sudo' command for Windows?

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?

Asked by: Guest | Views: 275
Total answers/comments: 5
Guest [Entry]

"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"
Guest [Entry]

"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."
Guest [Entry]

"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"
Guest [Entry]

"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"
Guest [Entry]

"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"