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 for business | Windows Server | User experience | Other
0 comments No comments
{count} votes

Answer accepted by question author
  1. Anonymous
    2021-06-07T21:53:02.897+00:00

    Give this one a go.
    https://4sysops.com/archives/create-a-list-of-local-administrators-with-powershell/

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

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Anonymous
    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

  2. Anonymous
    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.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.