How to check if a user is an administrator for a given server name?

trong2001 21 Reputation points
2021-06-07T21:14:01.92+00:00

Using the username, I want to find a way to check if that user is an administrator for a given remote server.

I found the following function online but it only checks if the user is an admin for the local machine.

Any help is appreciated!

function Test-Administrator
{
$user = [Security.Principal.WindowsIdentity]::GetCurrent();
(New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}

Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
12,107 questions
0 comments No comments
{count} votes

2 additional answers

Sort by: Most helpful
  1. Yuhan Deng 3,761 Reputation points Microsoft Vendor
    2021-06-08T02:19:52.39+00:00

    Hi,
    You might want to refer to this article:
    https://devblogs.microsoft.com/scripting/check-for-admin-credentials-in-a-powershell-script/
    Also PowerShell version higher than 5.1 (Windows Server 2016) contains Get-LocalGroupMember cmdlet.
    $user = "$env:COMPUTERNAME\$env:USERNAME"
    $group = 'Administrators'
    $isInGroup = (Get-LocalGroupMember $group).Name -contains $user

    Thanks for your time.
    Best regards,
    Danny


    If the Answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Dave Patrick 426.1K Reputation points MVP
    2021-06-09T02:08:32.577+00:00

    Just checking if there's any progress or updates?

    --please don't forget to upvote and Accept as answer if the reply is helpful--

    0 comments No comments