Created
June 26, 2018 13:15
-
-
Save JeremySkinner/6612e4c34f77aca8b9c464db64f2fb08 to your computer and use it in GitHub Desktop.
Powershell - elevate command (sudo)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Test-Administrator { | |
if (($PSVersionTable.PSVersion.Major -le 5) -or $IsWindows) { | |
$currentUser = [Security.Principal.WindowsPrincipal]([Security.Principal.WindowsIdentity]::GetCurrent()) | |
return $currentUser.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) | |
} | |
return $false; | |
} | |
function Elevate { | |
$psCommand = 'powershell.exe' | |
if($PSVersionTable.PSVersion.Major -gt 5) { | |
$psCommand = 'pwsh.exe' | |
} | |
if(Test-Administrator) { | |
if($args.Count) { | |
& $psCommand -NoProfile -Command "$args" | |
} | |
return | |
} | |
if(!$args.Count) { | |
# Just start a new prompt elevated. | |
Start-Process $psCommand -Verb RunAs -WorkingDirectory $pwd | |
} | |
else { | |
# Start it and pass in the command | |
Start-Process $psCommand -ArgumentList "-NoProfile -Command $args" -Verb RunAs -Wait -WorkingDirectory $pwd | |
} | |
} | |
Set-Alias sudo Elevate |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment