Skip to content

Instantly share code, notes, and snippets.

@andrewodri
Created January 23, 2015 21:02
Show Gist options
  • Save andrewodri/46da27dab1e705f71b9c to your computer and use it in GitHub Desktop.
Save andrewodri/46da27dab1e705f71b9c to your computer and use it in GitHub Desktop.
Self-elevating PowerShell script with administrator styling
$myWindowsID = [System.Security.Principal.WindowsIdentity]::GetCurrent();
$myWindowsPrincipal = New-Object System.Security.Principal.WindowsPrincipal($myWindowsID);
$adminRole = [System.Security.Principal.WindowsBuiltInRole]::Administrator;
Push-Location;
Set-Location HKCU:\Console;
if(!(Test-Path -Path ".\%SystemRoot%_system32_WindowsPowerShell_v1.0_powershell.exe")){
Set-Item ".\%SystemRoot%_system32_WindowsPowerShell_v1.0_powershell.exe";
}
Set-Location ".\%SystemRoot%_system32_WindowsPowerShell_v1.0_powershell.exe";
Set-ItemProperty . ColorTable00 -type DWORD -value 0x00562401;
Set-ItemProperty . ColorTable07 -type DWORD -value 0x00f0edee;
Set-ItemProperty . FaceName -type STRING -value "Lucida Console";
Set-ItemProperty . FontFamily -type DWORD -value 0x00000036;
Set-ItemProperty . FontSize -type DWORD -value 0x000c0000;
Set-ItemProperty . FontWeight -type DWORD -value 0x00000190;
Set-ItemProperty . HistoryNoDup -type DWORD -value 0x00000000;
Set-ItemProperty . QuickEdit -type DWORD -value 0x00000001;
Set-ItemProperty . ScreenBufferSize -type DWORD -value 0x0bb80078;
Set-ItemProperty . WindowSize -type DWORD -value 0x00320078;
Pop-Location;
if($myWindowsPrincipal.IsInRole($adminRole))
{
$Host.UI.RawUI.WindowTitle = "Administrator: " + $myInvocation.MyCommand.Definition;
Clear-Host;
}else{
$newProcess = New-Object System.Diagnostics.ProcessStartInfo "PowerShell";
$newProcess.Arguments = "& '" + $script:MyInvocation.MyCommand.Path + "'";
$newProcess.Verb = "runas";
[System.Diagnostics.Process]::Start($newProcess);
Exit;
}
$WindowSize = $Host.UI.RawUI.WindowSize;
$WindowSize.Width = 120;
$WindowSize.Height = 50;
$Host.UI.RawUI.WindowSize = $WindowSize;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment