Sunday, December 11, 2016

Testing whether a given user is an administrator with PowerShell

Given a user object $u, you can use this PowerShell line to check whether that user is an administrator, i.e. a member of the Administrators group:

(Get-LocalGroupMember 'Administrators' | ? {$_.SID -eq $u.SID}).Count -ne 0

User objects come from the Get-LocalUser or Get-LocalGroupMember cmdlets. The former lets you get a user by name, which is more useful here.

Based on my Super User answer.

No comments:

Post a Comment