Skip to content

Instantly share code, notes, and snippets.

@JeremySkinner
Created June 26, 2018 13:15
Show Gist options
  • Save JeremySkinner/6612e4c34f77aca8b9c464db64f2fb08 to your computer and use it in GitHub Desktop.
Save JeremySkinner/6612e4c34f77aca8b9c464db64f2fb08 to your computer and use it in GitHub Desktop.
Powershell - elevate command (sudo)
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