Created
August 14, 2020 19:13
-
-
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)
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 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