Elevate function in PSCX 1.1.1

Here’s a useful function especially if you are on Vista and have left UAC enabled.  If you install PSCX 1.1.1 and use the PSCX default profile you will get this function dot sourced in via the profile.  However if you don’t do either of those then here’s the function posted here for your benefit:
# —————————————————————————
### <Function name=’Elevate’>
### <Author>Keith Hill</Author>
### <Description>
### Runs the specified command via an elevated PowerShell instance on Vista.
### To get debug info, set $DebugPreference temporarily to ‘Continue’.
### </Description>
### <Usage>
### elevate
### elevate notepad c:\windows\system32\drivers\etc\hosts
### elevate gci c:\windows\temp
### elevate {gci c:\windows\temp | export-clixml tempdir.xml; exit}
### elevate {gci c:\windows\temp | export-clixml tempdir.xml; exit} |
###         %{$_.WaitForExit(5000)} | %{Import-Clixml tempdir.xml}
### </Usage>
### </Function>
# —————————————————————————
Set-Alias su Elevate -Option AllScope
function Elevate {
 if ($MyInvocation.PositionMessage -match ‘char:(\d+)’) {
  $ndx = [int]$matches[1]
 $setDirCmd = "[Environment]::CurrentDirectory = (set-location -LiteralPath ‘$pwd’ -PassThru).ProviderPath"
 $OFS = ","
 Write-Debug "`$MyInvocation.Line args index is $ndx; `$args are <$args>"
 if ($args[0] -is [Scriptblock]) {
        $script = $args[0]
        Write-Debug "Starting PowerShell with scriptblock: {$script}"
        start-process powershell.exe -verb runas -workingdir $pwd `
            -arguments "-noexit -command & {$setDirCmd; $script}" 
 elseif ($args[0].length -gt 0) {
        $startProcessArgs = $MyInvocation.Line.Substring($ndx)
        $app = get-command $args[0] | select -first 1 | ? {$_.CommandType -eq ‘Application’}
        if ($app) {
            $startProcessArgs = $startProcessArgs.Substring($args[0].Length).Trim()
            Write-Debug "Starting <$($app.Path)> with args: <$startProcessArgs>"
            start-process $app.Path -verb runas -workingdir $pwd -arguments $startProcessArgs 
        else {
            Write-Debug "Starting PowerShell with args: <$startProcessArgs>"
            start-process powershell.exe -verb runas -workingdir $pwd `
                -arguments "-noexit -command & {$setDirCmd; $startProcessArgs}"        
 else {
        Write-Debug "Starting Powershell without args"
        start-process powershell.exe -verb runas  -workingdir $pwd `
            -arguments "-noexit -command & {$setDirCmd}" 
This entry was posted in PSCX. Bookmark the permalink.

4 Responses to Elevate function in PSCX 1.1.1

  1. Shawn Dumas says:

    I get the Error…
    The term \’start-process\’ is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try again.At line:28 char:22+         start-process  <<<< powershell.exe -verb runas  -workingdir $pwd -arguments "-noexit -command & {$setDirCmd}"
    …when I try and run the Elevate function…

  2. Keith says:

    Shawn, Start-Process is one of the CMDLET\’s provided by the PowerShell Community Extensions project. This script (and alias) is provided by PSCX when you install it.  http://www.codeplex.com/powershellcx. 

  3. Shawn Dumas says:

    You know… I should have anticipated that response!
    I am aware of PSCX and have considered installing it many times but have decided not to yet. I was, however, very much in need of an su implementation and was very happy that you posted the source of the function. Is there an easy way that I can extract just the start-process script and not install all of PSCX?
    BTW: Thank you for your excellent blog and efforts in the PowerShell community!

  4. Keith says:

    If you install PSCX but uncheck the box to install the PSCX profile, the impact on your system is very minimal.  It will take up a bit of disk space and it adds an "Open PowerShell Prompt Here" context menu to Windows Explorer.  After that, all you need to do is execute Add-PSSnap PSCX and you will have access to Start-Process (along with a lot of other handy cmdlets).  Then this SU function should work. 

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