Here is a easy way to see who’s part of a local admin group remotely on a server or another machine. This is compatible with Server 2008 R2+.
CLS
$Server = "serverABC"
Invoke-Command -ComputerName $Server -ScriptBlock{
$members = Invoke-Expression -command "Net Localgroup Administrators"
$members[6..($members.Length-3)]
}
Here is an example of how you can use this to copy all the members of a Local admin group on server1 and replicate that same structure on server2
$CopyServer = "Server1"
$NewServer = "Server2"
$LocalAdminUsers = Invoke-Command -ComputerName $CopyServer -ScriptBlock{
$members = Invoke-Expression -command "Net Localgroup Administrators"
$members[6..($members.Length-3)]
}
Invoke-Command -ComputerName $NewServer -ScriptBlock{
foreach($user in $using:LocalAdminUsers)
{
Invoke-Expression -command "Net Localgroup Administrators $($user) /add" -ErrorAction SilentlyContinue
}
} -ErrorAction SilentlyContinue