Skip to main content

Check if script runs as admin

Check session for admin privileges

If you have to run your commands in your custom script as admin, it is essential for error handling to check if the current session is admin.

$isAdmin = (New-Object Security.Principal.WindowsPrincipal $CurrentUser).IsInRole([Security.Principal.SecurityIdentifier] "S-1-5-32-544")

if($isAdmin){
	Write-Output "PowerShell session is admin"	
}
if(!$isAdmin){
	Write-Output "PowerShell session is not admin"
}

Use case

This script can be used to combine with a Remote PowerShell Session to get local Configurations from other Windows Servers or Clients, which only can be accessed via local admin privileges or domain admin privileges.

More about this use case in docs entry "Authenticate to different host".