0

I'm looking for a cmd.exe equivalent to Linux's sudo !! and other bang-commands such as !x, !?x, !!:p and !!* mentioned here.

Outman
  • 103
  • 1
  • 1
  • 6
  • 1
    I don't think so, `cmd` is an ancient beast with a lot of unfixable problems due to backward compatibility. You don't even have an equivalent to `sudo` in Windows. Powershell is much more powerful and you can do the above things with `Get-History` – phuclv Dec 06 '18 at 14:19
  • You can try `cygwin` if you really miss Linux commands. – NotepadPlusPlus PRO Dec 06 '18 at 14:48
  • @Cricrazy cygwin is great, but my use case exclusively involves `cmd`. – Outman Dec 06 '18 at 15:30
  • The just released [gsudo v0.7.1](https://superuser.com/a/1527054/45898) suports `!!` on CMD. :) – Gerardo Grignoli Jun 12 '20 at 22:14

3 Answers3

2

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 !prefix elevates the last executed command that starts with prefix.
  • gsudo !?infix elevates 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

Gerardo Grignoli
  • 354
  • 1
  • 4
  • 12
0

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:

harrymc
  • 455,459
  • 31
  • 526
  • 924
  • Unfortunately this doesn't works in Windows 10. It will run as that user but without elevation/admin rights. – Gerardo Grignoli Feb 20 '20 at 20:26
  • @GerardoGrignoli: You are right that a Linux user will expect this, although Windows is different. I added links to some some self-elevation methods. – harrymc Feb 20 '20 at 21:16
0

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.

shawn
  • 875
  • 7
  • 15