Remove azuread\user from localgroup administrators

Good afternoon (US Central)

I, nor Atera AI, can seem to get it correct… to Remove azuread\user from localgroup administrators

Get-LocalGroupMember : Failed to compare two elements in the array.
At localadmin.ps1:8 char:12

  • $members = Get-LocalGroupMember -Group $localGroupName

  • CategoryInfo : NotSpecified: (:slight_smile: [Get-LocalGroupMember], InvalidOperationException
  • FullyQualifiedErrorId : An unspecified error occurred.,Microsoft.PowerShell.Commands.GetLocalGroupMemberCommand

.

Define the local group name

$localGroupName = “administrators”

Define the domain name

$domainName = “azuread”

Get the members of the local group

$members = Get-LocalGroupMember -Group $localGroupName

Loop through each member and remove if it is an Azure AD user

foreach ($member in $members) {
if ($member.ObjectClass -eq “User” -and $member.Name.StartsWith("$domainName")) {
Remove-LocalGroupMember -Group $localGroupName -Member $member.Name
}
}

.

Get-LocalGroupMember : Failed to compare two elements in the array.
At localadmin.ps1:5 char:12

  • $members = Get-LocalGroupMember -Group $adminGroup | Where-Object {$_ …

  • CategoryInfo : NotSpecified: (:slight_smile: [Get-LocalGroupMember], InvalidOperationException
    #NAME?

.

Set the variable $adminGroup to the name of the administrators group

$adminGroup = “Administrators”

Get all members of the administrators group and filter out any that start with “LocalAdmin”

$members = Get-LocalGroupMember -Group $adminGroup | Where-Object {$_.Name -notlike “LocalAdmin”}

Loop through each member and remove them from the administrators group

foreach ($member in $members) {
Remove-LocalGroupMember -Group $adminGroup -Member $member.Name
}

Output a message indicating the script has completed

Is anyone there??? Your assistance is appreciated.

I do believe doing the following might help you get some input:

  1. Post your entire script formatted as code.
  2. Ask a question or explain what you want and or what is not working.
  3. Post any errors you are getting formatted as code.

Created a new post. Maybe the admins will delete this one.