Skip to main content

windows-sudo

Like the Linux "sudo" command, but for Windows. Uses Python and pywin32 to run Administrator code.

Each command will present a User Account Control prompt to elevate the privilege for that command.

You can also launch an interactive Command (cmd), bash, or PowerShell window.

The command will execute in the current directory (unlike Window's sudo.exe.)

It also works on Linux, where it elevates through the system's own sudo and doesn't need pywin32 (pip installs pywin32 only on Windows). That lets a Python program use one API to re-run itself with elevated privileges, and hand its answers so far to the elevated copy, on either system. The salt-bevy system uses it this way, on Linux as well as Windows, to bootstrap Salt on a new machine.

Installation

  1. Install Python 3 (either for all users or single user) and .

  2. pip install windows-sudo

  3. windows-sudo-install

  4. Answer "Y" to the query about modifying PATH.

    (pip will install the windows-sudo-install command automatically as part of step 2 -- but it can't run it for you, since a pip install from a prebuilt wheel never executes arbitrary code, only windows-sudo-install itself can. It will request elevatation when needed.)

  5. Open a new cmd window -- sudo works there immediately (it installs a sudo.bat launcher, so no .py file association or PATHEXT change is needed). The one exception: if it just moved the native Windows sudo.exe on the system PATH (see the Windows 11 note below), log off and back on for that change to take effect everywhere.

Alternatively, to run from a source checkout instead: py -m pip install pywin32 pyyaml, then install_sudo.bat in place of steps 2-3 above.

During installation, it will offer to move the Python script directory in PATH ahead of the directory which contains Windows 11 sudo.exe. If you do not select that option, you will always need to type sudo.bat in order to run this utility. IMHO, this one has much better features.

Operation

Usually . . . sudo your command here

If you are forgetful . . . sudo --help or sudo /?

To get an interactive Administrator session . . .

sudo cmd, sudo bash, or sudo ps (PowerShell)

To pause the Administrator window so you can read the messages before they disappear. . .

sudo --pause your command here

Special commands

To edit the hard-to-find Windows version of /etc/hosts . . .

sudo --hosts

To permanently set environment variables from the command line . . .

sudo --set-user-env="arg1: val1, arg2: val2"

or

sudo --set-system-env="arg1: val1, arg2: val2"

(the above argument strings are in YAML format, and must be quoted because the spaces are part of the syntax.)

To remove an environment variable entirely, set its value to "None" . . .

sudo --set-user-env="arg: None"

Environment variables "PATH" and "PATHEXT" are a special case, and append their argument to the path. So, to add "C:\Some\Directory" to the search path, use . . .

sudo --set-system-env="PATH: C:\Some\Directory"

To remove one item from PATH or PATHEXT specifically (leaving the rest of the list alone), prepend a dash to its value, not its name . . .

sudo --set-system-env="PATH: -C:\Some\Directory"

Note the two dash forms are not interchangeable: "PATH: -C:\Some\Directory" removes just that one entry from the PATH list, while "arg: None" deletes the whole "arg" variable. A leading dash on the variable name itself (e.g. "-arg: val") is not special -- it is taken literally, as part of the name.

For lazy systems administrators who use Salt, any command beginning with "salt-" will be run with a pause . . .

sudo salt-call --local test.version

Or as a short cut, "--salt" will expand to "salt-call --local", so:

sudo --salt test.version

Last of all, sudo.py installs itself.

cd <the windows-sudo root directory>\windows_sudo
py sudo.py --install-sudo-command`

Windows 11 note

Recent Windows 11 builds (24H2+) can ship their own native sudo command in System32, which is normally found ahead of this package's version. When you run --install-sudo-command, it now detects that case and offers to move this package's install directory to the front of your PATH so sudo keeps resolving to this package instead. Answer "n" to leave the native command in control.

Testing

An automated go/no-go suite covers everything that needs no elevation (CLI smoke tests, arg parsing, quoting, etc.) . . .

py -m pytest tests/

For the elevated behavior itself, which only a human can judge, some toy scripts are provided for interactive testing under sudo . . .

sudo tests\manual\toy.py
sudo tests\manual\toy.bat
sudo --powershell ".\tests\manual\toy.ps1"

Each one reports its working directory, arguments, and whether it ended up elevated, then waits for a keypress so you can read the result.

Metadata

Release files for windows-sudo 2.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for windows-sudo 2.1.2
File Size Uploaded Provenance Actions
windows_sudo-2.1.2.tar.gz 20.2 kB With provenance Details for windows_sudo-2.1.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for windows-sudo 2.1.2
File Interpreter ABI Platform Actions
windows_sudo-2.1.2-py3-none-any.whl Python 3 none any Details for windows_sudo-2.1.2-py3-none-any.whl

Total release size: 36.7 kB

Release files / windows_sudo-2.1.2.tar.gz

Download URL windows_sudo-2.1.2.tar.gz
Size 20.2 kB
Tags Source
SHA-256 checksum
How to use checksums
8ff0fd54ec8981b4acb1735590667e5fda32704f4df0f21d90b109e77dc85d32
BLAKE2b-256 checksum
How to use checksums
bc02056163e052a694b05ba3126fbffcc70afb035b01b230b5f124d93ab18538
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 3, 2026.

Transparency log

Release files / windows_sudo-2.1.2-py3-none-any.whl

Download URL windows_sudo-2.1.2-py3-none-any.whl
Size 16.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7b23f665a08c78e7b691b5f398f19fb7bb0d881336d625295ab158db40ca7edb
BLAKE2b-256 checksum
How to use checksums
fbd82f31dce097c5446adc4683ef8ccf23a1735198b23350575e33f70c4468a5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 3, 2026.

Transparency log

Security for windows-sudo 2.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for windows_sudo-2.1.2.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 3, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page