PowerShell: UAC, SUDO, Elevation, Access to the path ‘C:\Program Files’ is denied.

Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

Powershell on Windows Vista and Windows 7 runs restricted and not-elevated by default. Microsoft did a good job when they invented the so called UAC (User Account Control). As ususal, Microsoft started something new and did not finish it yet – the product is still not feature-complete. Well, probably it’s their philosophy to create thousands of products in a vertical manner, the user then is left all alone finding out how to dive into the features from a horizontal perspective…

What i mean: PowerShell runs restricted and has NO SUDO command. For sake of christ, there is a workaround.

First, find out the location of your profile.ps1. it easy as:


PS C:\> $profile
C:\Users\

Probably this script does not exist by default. At least on my systems this was the case. So create it.
When created / if it does exist do:

Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.


PS C:\> notepad.exe $profile

This should open your profile.ps1 script in notepad..
Edit this Script to include:


function elevate-process
{
$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);
}

set-alias sudo elevate-process;

Now you’re almost done. After starting a new PowerShell session – even a non-elevated one – you can run commands elevated by using the sudo command.
For Illustration:

PS C:\> sudo notepad.exe

If the new PowerShell session should display a warning regarding the execution of scripts, check out my older posts (using the site search), i’ve postet a solution for that.

1 thought on “PowerShell: UAC, SUDO, Elevation, Access to the path ‘C:\Program Files’ is denied.

  1. when trying to run notepad.exe $profile

    results in

    The system cannot find path specified.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.