Skip to content

Instantly share code, notes, and snippets.

@TangChr
Last active March 15, 2022 20:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save TangChr/2ab5f0aefd7d523f6d99e57d5d7774ce to your computer and use it in GitHub Desktop.
Save TangChr/2ab5f0aefd7d523f6d99e57d5d7774ce to your computer and use it in GitHub Desktop.
Check if Command window is running with administrator permissions
@echo off
goto check_Permissions
:check_Permissions
echo Administrative permissions required. Detecting permissions...
net session >nul 2>&1
if %errorLevel% == 0 (
echo Success: Administrative permissions confirmed.
) else (
echo Failure: Current permissions inadequate.
)
pause >nul
function Test-IsAdmin {
try {
$identity = [Security.Principal.WindowsIdentity]::GetCurrent()
$principal = New-Object Security.Principal.WindowsPrincipal -ArgumentList $identity
return $principal.IsInRole( [Security.Principal.WindowsBuiltInRole]::Administrator )
} catch {
throw "Failed to determine if the current user has elevated privileges. The error was: '{0}'." -f $_
}
<#
.SYNOPSIS
Checks if the current Powershell instance is running with elevated privileges or not.
.EXAMPLE
PS C:\> Test-IsAdmin
.OUTPUTS
System.Boolean
True if the current Powershell is elevated, false if not.
#>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment