Powershell script. Remove all the local admin users from a CSV

FUENTES Jose 21 Reputation points
2022-08-08T10:02:41.957+00:00

I have a CSV file with all users I want to delete from the administrator local group. So for instance, in one workstation, in the local administrator group has three users (Jose, Mary, Anna) and in the CSV file has Jose and Mary, in the local administrator group after launching the script should be only Anna. I hope I explain properly

I've done this script in PowerShell, but doesn't work.

Import-Csv -path "C:\Temp\Files\Users.csv" | ForEach-Object {Remove-localgroupmember -Identity "Administrators" -Members $_.'Account' -Confirm:$false}

Please anybody helps me??

Windows for business Windows Server User experience PowerShell
{count} votes

Accepted answer
  1. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2022-09-28T16:35:51.343+00:00

    Hi @FUENTES Jose ,

    thanks for the feedback.

    Does the answer help to solve the issue?
    If you found the answer helpful, it would be great if you please mark it "Accept as answer". This will help others to find answers in Q&A

    ----------

    Regards
    Andreas Baumgarten

    0 comments No comments

18 additional answers

Sort by: Most helpful
  1. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2022-08-08T11:24:15.48+00:00

    Please try this:

    Import-Csv -path "C:\Temp\Files\Users.csv" | ForEach-Object {Remove-localgroupmember -Group "Administrators" -Members $_.'Account' -Confirm:$false}  
    

    There is no -Identity parameter ;-)
    https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.localaccounts/remove-localgroupmember?view=powershell-5.1

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

  2. FUENTES Jose 21 Reputation points
    2022-08-08T11:30:42.623+00:00
    PS C:\WINDOWS\system32> Import-Csv -path "C:\Temp\Files\Users.csv" | ForEach-Object {Remove-localgroupmember -Group "Administrators" -Members $_.'Account' -Confirm:$false}  
    Remove-LocalGroupMember : No se encuentra ningún parámetro que coincida con el nombre del parámetro 'Members'.  
    En línea: 1 Carácter: 110  
    + ... ect {Remove-localgroupmember -Group "Administrators" -Members $_.'Acc ...  
    +                                                          ~~~~~~~~  
        + CategoryInfo          : InvalidArgument: (:) [Remove-LocalGroupMember], ParameterBindingException  
        + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.RemoveLocalGroupMemberCommand  
       
    Remove-LocalGroupMember : No se encuentra ningún parámetro que coincida con el nombre del parámetro 'Members'.  
    En línea: 1 Carácter: 110  
    + ... ect {Remove-localgroupmember -Group "Administrators" -Members $_.'Acc ...  
    +                                                          ~~~~~~~~  
        + CategoryInfo          : InvalidArgument: (:) [Remove-LocalGroupMember], ParameterBindingException  
        + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.RemoveLocalGroupMemberCommand  
    
    0 comments No comments

  3. FUENTES Jose 21 Reputation points
    2022-08-08T11:43:31.627+00:00

    229108-image.png

    229193-image.png

    0 comments No comments

  4. FUENTES Jose 21 Reputation points
    2022-08-08T11:44:38.753+00:00

    I tested it with "Administradores" and "Administrators" in group member

    0 comments No comments

Your answer

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