How to run 'sudo' command in windows

Windows

Windows Problem Overview


How would I run the following command in windows:

$ sudo django-admin.py startproject NEW

?

Windows Solutions


Solution 1 - Windows

There is no sudo command in Windows. The nearest equivalent is "run as administrator."

You can do this using the runas command with an administrator trust-level, or by right-clicking the program in the UI and choosing "run as administrator."

Solution 2 - Windows

All the answers explain how to elevate your command in a new console host. So, I wrote: gsudo to behave like Unix/Linux sudo, allowing to execute the command inside the current console.

gsudo

Most relevant features:
  • Elevates in the current console (no new window)
  • Elevates cmd commands, but also PowerShell / WSL / Git-Bash / cygwin / Msys / commands natively.
  • Optional: Credentials Cache (Elevate many times with only one UAC popup)
Installation

Demo

gsudo demo

Solution 3 - Windows

Open notepad and paste this code:

@echo off
powershell -Command "Start-Process cmd -Verb RunAs -ArgumentList '/c cd /d %CD% && %*'"
@echo on

Then, save the file as sudo.cmd. Copy this file and paste it at C:\Windows\System32 or add the path where sudo.cmd is to your PATH Environment Variable.

When you open command prompt, you can now run something like sudo start ..

If you want the admin command prompt window to stay open when you run the command, change the code in notepad to this:

@echo off
powershell -Command "Start-Process cmd -Verb RunAs -ArgumentList '/k cd /d %CD% && %*'"
@echo on

Explanation:

powershell -Command runs a powershell command.

Start-Process is a powershell command that starts a process, in this case, command prompt.

-Verb RunAs runs the command as admin.

-Argument-List runs the command with arguments.

Our arguments are '/c cd /d %CD% && %*'. %* means all arguments, so if you did sudo foo bar, it would run in command prompt foo bar because the parameters are foo and bar, and %* returns foo bar. cd /d %CD% is a command to go to the current directory. This will ensure that when you open the elevated window, the directory will be the same as the normal window. the && means that if the first command is successful, run the second command.

The /c is a cmd parameter for closing the window after the command is finished, and the /k is a cmd parameter for keeping the window open.

Credit to Adam Plocher for the staying in the current directory code.

Solution 4 - Windows

I've created wsudo, an open-source sudo-like CLI tool for Windows to run programs or commands with elevated right, in the context of the current directory. It's available as a Chocolatey package.

I use it a lot for stuff like configuring build agents, admin things like sfc /scannow, dism /online /cleanup-image /restorehealth or simply for installing/updating my local Chocolatey packages. 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 

For more details, visit the GitHub repro.

Solution 5 - Windows

runas command requires the users to type password. If you don't want to type password and want to just click the UAC dialog, use Start-Process -Verb runas in PowerShell instead of runas command.

see: http://satob.hatenablog.com/entry/2017/06/17/013217

Solution 6 - Windows

You normally wouldn't, since you wouldn't run it under *nix regardless. Do development in a user directory, and deploy afterwards to system directories.

Solution 7 - Windows

in Windows, you can use the runas command. For linux users, there are some alternatives for sudo in windows, you can check this out

http://helpdeskgeek.com/free-tools-review/5-windows-alternatives-linux-sudo-command/

Solution 8 - Windows

There kind of is. I created Sudo for Windows back in 2007? 08? Here's the security paper I wrote about it - https://www.sans.org/reading-room/whitepapers/bestprac/sudo-windows-sudowin-1726. Pretty sure http://sudowin.sf.net still works too.

Solution 9 - Windows

Solution 10 - Windows

There is no sudo command in case of windows and also there is no need to put any $. For installing Angular CLI through node.js command prompt in windows, I just wrote npm install -g @angular/cli and then pressed Enter. It worked fine.

Solution 11 - Windows

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

Solution 12 - Windows

I think I tried steps below after doing some research & succeeded

1.Install scoop using powershell 3 (iex (new-object net.webclient).downloadstring('https://get.scoop.sh';)) 2. do scoop install --global sudo

  1. make sure paths (C:\Users<username>\scoop\shims & C:\ProgramData\scoop\shims) added in environmental path variable.

Solution 13 - Windows

open the console as a administrator. Right Click on the command prompt or bash -> more and select "run as administrator"

Solution 14 - Windows

In Windows Powershell you can use Start-Process command with option -Verb RunAs to start and elevated process. Here is my sudo function example:

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

Ex: Open hosts file as Admin in notepad

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

Solution 15 - Windows

To just run a command as admin in a non-elevated Powershell, you can use Start-Process directly, with the right options, particularly -Verb runas.

It's a lot more convoluted than sudo, particularly because you can't just re-use the previous command with an additional option. You need to specify the arguments to your command separately.

Here is an example, using the route command to change the gateway :

This fails because we are not in an elevated PS:

> route change 0.0.0.0 mask 0.0.0.0 192.168.1.3
The requested operation requires elevation.

This works after accepting the UAC:

> Start-Process route -ArgumentList "change 0.0.0.0 mask 0.0.0.0 192.168.1.3" -Verb runas

Or for a command that requires cmd.exe:

> Start-Process cmd -ArgumentList "/c other_command arguments ..." -Verb runas

Solution 16 - Windows

Using the sudo command for window users gives you the following suggestions:

First, install the scoop package management tool and execute the following commands:

enter image description here

Then use scoop to install the sudo command:

enter image description here

Finally, you can execute the command with sudo:

enter image description here

Solution 17 - Windows

I am glad to help you.

To make a sudo command follow the steps :-

  1. Make a folder env in C:\ Drive.
  2. Make a folder inside the env folder named as bin.
  3. Now add C:\env\bin to your PATH.
  4. Create a file sudo.bat in C:\env\bin.
  5. Edit it with notepad and write this code :-
    https://gist.github.com/coder-examples/5ca6c141d6d356ddba4e356d63fb2c13

Hope this will help you

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDavid542View Question on Stackoverflow
Solution 1 - WindowsBlueRaja - Danny PflughoeftView Answer on Stackoverflow
Solution 2 - WindowsGerardo GrignoliView Answer on Stackoverflow
Solution 3 - WindowsCME1 CrewmemberView Answer on Stackoverflow
Solution 4 - WindowsnoseratioView Answer on Stackoverflow
Solution 5 - WindowsSATO YusukeView Answer on Stackoverflow
Solution 6 - WindowsIgnacio Vazquez-AbramsView Answer on Stackoverflow
Solution 7 - WindowsPeter AndrewView Answer on Stackoverflow
Solution 8 - WindowsakutzView Answer on Stackoverflow
Solution 9 - WindowsJaehView Answer on Stackoverflow
Solution 10 - WindowsSaurabh KumarView Answer on Stackoverflow
Solution 11 - WindowsceztkoView Answer on Stackoverflow
Solution 12 - WindowsPawan GangwaniView Answer on Stackoverflow
Solution 13 - WindowsJames NView Answer on Stackoverflow
Solution 14 - WindowsDavid HatchView Answer on Stackoverflow
Solution 15 - WindowsmivkView Answer on Stackoverflow
Solution 16 - WindowsazjhongView Answer on Stackoverflow
Solution 17 - WindowsAayush ShahView Answer on Stackoverflow