Created
July 11, 2018 09:32
-
-
Save demius/4c0ee0acc75430fa0b89201e6067ffc0 to your computer and use it in GitHub Desktop.
Powershell: Check for elevated permissions
This file contains hidden or 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
# 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