I'm looking for a cmd.exe equivalent to Linux's sudo !! and other bang-commands such as !x, !?x, !!:p and !!* mentioned here.
- 103
3 Answers
You can try gsudo, a sudo for windows that allows either to run commands with elevated permissions on the current console, to elevate the current shell, or launch elevated commands on a new console.
Examples
gsudo {command} [arguments]
gsudo md "C:\Program Files\MyApp"
spawn the current shell (Cmd/PowerShell/PSCore) in a new console window
gsudo -n
spawn PowerShell in a new console window
gsudo -n powershell
UPDATE:
Since gsudo v0.7.1, it supports the Unix-sudo Bang Bang syntax, on CMD:
gsudo !!elevates the last executed command.gsudo !prefixelevates the last executed command that starts with prefix.gsudo !?infixelevates the last executed command that contains infix.
Installation
- Install with Scoop:
scoop install gsudo - Install with Chocolatey:
choco install gsudo
Manuall installation methods, docs & source at https://github.com/gerardog/gsudo
- 394
The sudo equivalent is the runas command, used to execute a program under a different user account.
Example of use :
runas /user:an-administator-account "cmd.exe /C mycommand"
Windows is different than Linux in that by default even an admin account doesn't run with elevation, although, unlike a non-admin account, it can elevate itself.
For methods of self-elevation see:
- 498,455
To answer the other half of the question, you can use doskey to view or parse the command history. Doskey is installed and active by default on all supported versions of Windows.
This shows your command history:
doskey /history
You could parse and push the results to runas, but wrapping it in a batch file would make it a lot simpler at the prompt.
- 965