Tome's Land of IT

IT Notes from the Powertoe – Tome Tanasovski

Powerbits #6 PowerShell Runas Function for UAC

I was just browsing Osin’s latest contribution to the community, pseventing plus, which allows you to add hotkey events to your PowerShell sessions.  I was looking at his samples and found the handy one to start notepad with elevated privileges to open your hosts file.  If you are unaware, there is a -Verb parameter with Start-Process that will accept “Runas” as an argument to start a new process with elevated privileges.  If you wanted to open your hosts file with notepad the command would be:

Start-Process -Verb Runas notepad c:\windows\system32\drivers\etc

This is already pretty well documented, but I realized that I was foolish because I never wrapped this in a function and put it in my profile.  so I did it, and it is now here for you as a powerbit.

function runas {
    param(
        [Parameter(Mandatory=$true,Position=0)]
        [string]$FilePath,
        [Parameter(Mandatory=$false,Position=1)]
        [string] $ArgumentList
    )
    if ($ArgumentList) {
        start -Verb runas -FilePath $FilePath -ArgumentList $ArgumentList
    } else {
        start -Verb runas $FilePath
    }
}

Add this to your profile and you can use it like this:

runas notepad c:\windows\system32\drivers\etc

Feel free to change the name from runas if you don’t like that name.  For some, the name is probably confusing if you were used to using runas from a command prompt. I’m used to typing ‘start -verb runas’ so it makes sense for me.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: