Skip to content

Instantly share code, notes, and snippets.

@tonylea
Created August 22, 2016 10:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tonylea/c19107d92412e724650eeb1e1edb24fb to your computer and use it in GitHub Desktop.
Save tonylea/c19107d92412e724650eeb1e1edb24fb to your computer and use it in GitHub Desktop.
Script block to self-elevate a script
# Script found at https://www.autoitscript.com/forum/topic/174609-powershell-script-to-self-elevate/
# Test if admin
function Test-IsAdmin()
{
# Get the current ID and its security principal
$windowsID = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$windowsPrincipal = new-object System.Security.Principal.WindowsPrincipal($windowsID)
# Get the Admin role security principal
$adminRole=[System.Security.Principal.WindowsBuiltInRole]::Administrator
# Are we an admin role?
if ($windowsPrincipal.IsInRole($adminRole))
{
$true
}
else
{
$false
}
}
# Get UNC path from mapped drive
function Get-UNCFromPath
{
Param(
[Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)]
[String]
$Path)
if ($Path.Contains([io.path]::VolumeSeparatorChar))
{
$psdrive = Get-PSDrive -Name $Path.Substring(0, 1) -PSProvider 'FileSystem'
# Is it a mapped drive?
if ($psdrive.DisplayRoot)
{
$Path = $Path.Replace($psdrive.Name + [io.path]::VolumeSeparatorChar, $psdrive.DisplayRoot)
}
}
return $Path
}
# Relaunch the script if not admin
function Invoke-RequireAdmin
{
Param(
[Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)]
[System.Management.Automation.InvocationInfo]
$MyInvocation)
if (-not (Test-IsAdmin))
{
# Get the script path
$scriptPath = $MyInvocation.MyCommand.Path
$scriptPath = Get-UNCFromPath -Path $scriptPath
# Need to quote the paths in case of spaces
$scriptPath = '"' + $scriptPath + '"'
# Build base arguments for powershell.exe
[string[]]$argList = @('-NoLogo -NoProfile', '-ExecutionPolicy Bypass', '-File', $scriptPath)
# Add
$argList += $MyInvocation.BoundParameters.GetEnumerator() | Foreach {"-$($_.Key)", "$($_.Value)"}
$argList += $MyInvocation.UnboundArguments
try
{
$process = Start-Process PowerShell.exe -PassThru -Verb Runas -Wait -WorkingDirectory $pwd -ArgumentList $argList
exit $process.ExitCode
}
catch {}
# Generic failure code
exit 1
}
}
# Relaunch if not admin
Invoke-RequireAdmin $script:MyInvocation
# Running as admin if here
$wshell = New-Object -ComObject Wscript.Shell
$wshell.Popup("Script is running as admin", 0, "Done", 0x1) | Out-Null
@vivek1986
Copy link

@tonylea Why not just use a small, never-need-change almost one-liner for self-elevating .ps1 script to Admin and also not forget your current folder/directory:

$Loc = Get-Location
"Security.Principal.Windows" | % { IEX "( [ $_`Principal ] [$_`Identity ]::GetCurrent() ).IsInRole( 'Administrator' )" } | ? {
    $True | % { $Arguments =  @('-NoProfile','-ExecutionPolicy Bypass','-NoExit','-File',"`"$($MyInvocation.MyCommand.Path)`"","\`"$Loc\`"");
    Start-Process -FilePath PowerShell.exe -Verb RunAs -ArgumentList $Arguments; } }

(Get-Location).ToString()
## Any PS code that needs elevation
Read-Host

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment