Skip to content

Instantly share code, notes, and snippets.

@ellisgeek
Last active October 7, 2024 00:54
Show Gist options
  • Save ellisgeek/2a0821ebf9bb983e04dc to your computer and use it in GitHub Desktop.
Save ellisgeek/2a0821ebf9bb983e04dc to your computer and use it in GitHub Desktop.
Automatically relaunch Powershell script as Administrator
#region Elevate
function Elevate {
<#
.SYNOPSIS
Automatically (re)launch Powershell script as Administrator including parameters
.PARAMETER ScriptPath
Path to the script that should be launched. Defaults to the current script
.PARAMETER Parameters
A Hashtable of parameters that should be passed to the elevated script, where the "key" is the
parameter name and the "value" is the parameter value
.PARAMETER Exit
End the current powershell session after launching the script
.EXAMPLE
Relaunch the current script as Administrator passing along any parameters passed to the
current instance and then end the current session.
Elevate -Parameters $PSBoundParameters -Exit
.LINK https://gist.github.com/ellisgeek/2a0821ebf9bb983e04dc
.NOTE
==================================== About ====================================
===============================================================================
Author: Elliott Saille <[email protected]>
Date: May 19, 2015
=================================== LICENSE ===================================
===============================================================================
This Source Code Form is subject to the terms of the Mozilla Public License,
v. 2.0. If a copy of the MPL was not distributed with this file, You can
obtain one at http://mozilla.org/MPL/2.0/.
===============================================================================
#>
param
(
[parameter(Position = 0)]
[string]$ScriptPath = $script:MyInvocation.MyCommand.Path,
[parameter(Position = 1, Mandatory = $true)]
[hashtable]$Params
)
# This will hold our argument string that gets passed to the new powershell instance.
$arg = ""
# Only iterate over the params object if we need to
if (-not [string]::IsNullOrEmpty($Params)) {
# Iterate over the parameters the parent script got and turn them into a string of arguments
# to pass to the new session
Foreach ($key in $params.Keys) {
$value = $params[$key]
$arg += "-$key $value"
}
}
# Provide Feedback
Write-Host("Relaunching script as Administrator!")
# Only run if we aren't running as Administrator
If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(`
[Security.Principal.WindowsBuiltInRole] "Administrator")) {
Write-Verbose("Restarting script with Administrator rights")
# Run script in a new session as Administrator.
Start-Process -FilePath powershell.exe -ArgumentList @("-File `"$ScriptPath`" $arg") `
-Verb runas #-WindowStyle 'Hidden' # Uncomment this line if you are using
# .NET Forms to hide the Powershell
# window that is spawned by the new
# session
Write-Verbose("Ending current Session")
# Return non zero exit code that can be used to check if script was relaunched
$host.SetShouldExit(42)
# End current session and let the new one take over
Exit
}
}
# Elevate this script, passing any arguments to the new session
Elevate -Params $PSBoundParameters
#endregion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment