Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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