Skip to content

Instantly share code, notes, and snippets.

@BanterBoy
Created August 14, 2020 19:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BanterBoy/ee54937165b8390c75e4bb7ccae731a5 to your computer and use it in GitHub Desktop.
Save BanterBoy/ee54937165b8390c75e4bb7ccae731a5 to your computer and use it in GitHub Desktop.
Function used in conjunction with Test-IsAdmin to start a new Admin Shell (runs powershell or pwsh as admin)
function New-AdminShell {
<#
.Synopsis
Starts an Elevated PowerShell Console.
.Description
Opens a new PowerShell Console Elevated as Administrator. If the user is already running an elevated
administrator shell, a message is displayed in the console session.
.Example
New-AdminShell
#>
$Process = Get-Process | Where-Object { $_.Id -eq "$($PID)" }
if (Test-IsAdmin = $True) {
Write-Warning -Message "Admin Shell already running!"
}
else {
if ($Process.Name -eq "powershell") {
Start-Process -FilePath "powershell.exe" -Verb runas -PassThru
}
if ($Process.Name -eq "pwsh") {
Start-Process -FilePath "pwsh.exe" -Verb runas -PassThru
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment