‘sudo’ for Powershell, sorta

Using powershell one thing I found annoying is just could’t figure out how to run processes with elevated rights without having to start a new, elevated powershell. I was looking for a sudo kind of command/script.

I was hitting the big lower case g (google) and found this. Thanks to Tsukasa I finally found a ‘sudo’ for powershell.

I just had to figure out a why to get it to work. Probably there are others looking for the same I thought I might just summarise the steps it took to make it work.

Since I don’t know what editor is available on your system we’re going to use notepad.exe. Personally I’m using ‘e’, there is a free trial but it’s pretty much worth the money. So you might think about buying it.

let’s start with…

step 0.5 , creating a script: mount.

 

It’s no really necessary but might save you some time, so if you don’t care just skip to step 1.

First we’re going to create a directory to put all your scripts in

New-Item -ItemType directory $HOME\Documents\Scripts

After that we need to mount that directory so it’s easily accessible from everywhere within PS using the New-PSDrive cmdlet. The one draw back of that cmdlet is that all mounted directories only last as long as you current PS session. We need it to stay mounted, or at least get mounted at the beginning of every new session and there is a simple way to solve the problem. Put the mount command in your ps-profile. if you never bothered with it there probably isn’t a profile file yet, but Microsoft thought ahead and created a variable for it.

notepad.exe $PROFILE

and put this in it:

New-PSDrive -name script -PSProvider FileSystem -Root $HOME\Documents\Scripts

save, exit.

restart your powershell session by typing powershell into powershell. You should see the directory getting mounted, we’re good to go now.

cd script:

will take you to the script directory.

step 1, getting the script

 

Go to your script directory or any other directory where you want to put your script and type

notepad.exe sudo.ps1

Notepad will appear and you’ll need to put the following into it

1
2
3
4
5
6
7
8
9
$file, [string]$arguments = $args;
if([System.IO.File]::Exists("$(get-location)\$file"))
{
$file = "$(Get-Location)\$file";
}
$psi = new-object System.Diagnostics.ProcessStartInfo $file;
$psi.Arguments = $arguments;
$psi.Verb = "runas";
[System.Diagnostics.Process]::Start($psi);

save, exit

The script is already working, and you can run it from everywhere by typing the whole path and since that’s a major PITA we’re going to assign an alias.

step 2, assigning an alias

 

Open your PS profile

notepad.exe $PROFILE

adding the following line

New-Alias -Name sudo 'script:\sudo.ps1'

(or the respective directory)

save, exit

restart the session.

Now you’re able to start any application with elevated rights. e.g.

sudo notepad.exe

or

sudo powershell

to get a elevated shell but I’ve yet to find a way to make the elevated shell being opened in the working dir where the command was executed.

You might also like

9 thoughts on “‘sudo’ for Powershell, sorta

  1. Could preserve the working directory if you add:

    $psi.WorkingDirectory = Get-Location

    Thanks for this blog entry, very usefull

    ;-)

  2. Pingback: sudo for Powershell | Tech Haus

  3. @pezhore Thank you, looks good. Can’t wait to try it. I’ll update the post in the next couple of days and add a link to the pastebin and your site. Credit where credit is due. Speaking of which, instead of mrigns you could add my real name, Julian Saraceni.

  4. Hi,

    The above script doesn’t seem to work for aliases and functions. I’ve made a simple version below which has been working for me and handles aliases and functions.

    function sudo()
    {
    if ($args.Length -eq 1)
    {
    start-process $args[0] -verb “runAs”
    }
    if ($args.Length -gt 1)
    {
    start-process $args[0] -ArgumentList $args[1..$args.Length] -verb “runAs”
    }
    }

    Just add the above to your $profile, and you should be good.