Home > IT > ‘sudo’ for Powershell, sorta

‘sudo’ for Powershell, sorta

February 24th, 2009

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.

Popularity: 59% [?]

  • email
  • Digg
  • StumbleUpon
  • Yahoo! Buzz
  • TwitThis
  • Technorati
  • Furl
  • del.icio.us
  • Facebook
  • Mixx

IT , ,

  1. David Abella
    July 6th, 2009 at 02:46 | #1

    Could preserve the working directory if you add:

    $psi.WorkingDirectory = Get-Location

    Thanks for this blog entry, very usefull

    ;-)

  2. thomas
    November 20th, 2009 at 23:47 | #2

    Thanks !

  3. May 25th, 2010 at 17:18 | #3

    Hey, if you’re interested, I updated your script/added comments/functionality.

    Now it’ll take the -ps flag to directly run powershell scripts in an elevated terminal & change to the current working directory.

    http://pastebin.com/VWaYXBY5

    Cheers!

  4. May 26th, 2010 at 11:33 | #4

    @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.

  5. dunno
    June 18th, 2010 at 21:43 | #5

    hot to do with explorer.exe?
    it seems dont work

  1. May 25th, 2010 at 17:51 | #1