Manually running a .ps1 in an administrator (elevated) shell

I have a seemingly straightforward question: How can I manually run a .ps1 script on Server 2012 R2 and have it open in an administrator elevated shell? I am right clicking and clicking "Run with Powershell" on the .ps1 file.

My environment:

Two Server 2012 R2 machines in the same domain in the same OU. Both are full GUI installs. Both have UAC set to "default".

The discrepancy:

One of the servers will run any and all .ps1 files in an administrator elevated shell. The other server will run any and all .ps1 files in a non-administrator, standard shell. I have no idea what the differences are between the two servers. Neither are running any custom Powershell profiles.

The following registry keys are all identical between the two servers:








What am I missing?


A quick foray with Google ended me up with a posting on Ben Armstrong's blog where he posted code that auto-elevated a script if it was needed. Here's the code he posted that seems perfect for your needs:

# Get the ID and security principal of the current user account
 $myWindowsPrincipal=new-object System.Security.Principal.WindowsPrincipal($myWindowsID)

 # Get the security principal for the Administrator role

 # Check to see if we are currently running "as Administrator"
 if ($myWindowsPrincipal.IsInRole($adminRole))
    # We are running "as Administrator" - so change the title and background color to indicate this
    $Host.UI.RawUI.WindowTitle = $myInvocation.MyCommand.Definition + "(Elevated)"
    $Host.UI.RawUI.BackgroundColor = "DarkBlue"
    # We are not running "as Administrator" - so relaunch as administrator

    # Create a new process object that starts PowerShell
    $newProcess = new-object System.Diagnostics.ProcessStartInfo "PowerShell";

    # Specify the current script path and name as a parameter
    $newProcess.Arguments = $myInvocation.MyCommand.Definition;

    # Indicate that the process should be elevated
    $newProcess.Verb = "runas";

    # Start the new process

    # Exit from the current, unelevated, process

 # Run your code that needs to be elevated here
 Write-Host -NoNewLine "Press any key to continue..."
 $null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

Need Your Help

Code runs well on Linux but gives a different output on Windows OS

c++ windows linux console mingw

I have been wondering why the code below is giving me different output on Windows OS. Meanwhile, the same code works well on Ubuntu.

Best way to display pdf document in application

c# pdf webforms ghostscript

I'm looking for the best way to display pdf document on a website. Surely I need to convert it to jpeg or gif for the browser to handle it. I read few posts but most refer to GhostScript and its