Skip to content

Instantly share code, notes, and snippets.

@demius
Created July 11, 2018 09:32
Show Gist options
  • Save demius/4c0ee0acc75430fa0b89201e6067ffc0 to your computer and use it in GitHub Desktop.
Save demius/4c0ee0acc75430fa0b89201e6067ffc0 to your computer and use it in GitHub Desktop.
Powershell: Check for elevated permissions
# Check whether we are running in elevated mode
function Test-IsInElevatedMode {
<#
.DESCRIPTION
Checks to see whether the current Powershell prompt is running in elevated mode
.EXAMPLE
Test-IsInElevatedMode
.OUTPUTS
Returns $true when running in elevated mode, otherwise $false
#>
$requiredRole = [System.Security.Principal.WindowsBuiltInRole]::Administrator
$identity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$principal = New-Object System.Security.Principal.WindowsPrincipal($identity)
if($principal.IsInRole($requiredRole) -eq $false){
Write-Warning "This script requires elevated permissions to execute. Please run the Powershell console as Administrator."
exit
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment