• Home
  • About
  • Contact
  • Speaking

Run sudo in Windows

by Piotr Stapp — 09 Feb 2015

It is really frustrating when you need UAC to run one command. You are doing you stuff and you have to launch a new console (cmd or PowerShell) run one command, close it (or not) and start working again.
In Linux you have one magic word: sudo and you do not have to think about it. Of course we can disable UAC, but it is not recommended :)

But on Friday at least I found a magic sudo command for windows. Simple, plain PowerShell script, just to put in $PROFILE in PowerShell

function sudo  
{
    $file, [string]$arguments = $args;
    $psi = new-object System.Diagnostics.ProcessStartInfo $file;
    $psi.Arguments = $arguments;
    $psi.Verb = "runas";
    $psi.WorkingDirectory = get-location;
    [System.Diagnostics.Process]::Start($psi);
}

Now we can run everything in PowerShell with sudo command arg1 arg2 argX. Hurray :)

Update

The above works only with programs and have a problem with PowerShell commands. The simple workaround is to use sudo !! (run last command as sudo). I describe this in the sudo !! - run last command in elevated PowerShell prompt


Follow @ptrstpp950

0%
Run sudo in Windows
words - read.
Recent Posts:

Comments

comments powered by Disqus
Piotr Stapp Author

Piotr Stapp

Developer, engineer, craftsmen, speaker & biker. User of all useful technologies. Believe in people not papers. DevOps & automation adept. In love with web technologies. I speak here for myself only.

Previous post Using poshgit @VisualStudio Git in Visual Studio As You probably know Visual Studio has integrated git client. It is really cool thing.…
Next post Bad JSON escape sequence: \v - the war stories Introduction Yesterday I did a small C# program for hash data in JSON. The are many files, which contains…
All content copyright Reset your code © 2015 • All rights reserved.
Proudly published with Ghost