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-12T14:44:55.963+00:00

    Hi @FUENTES Jose ,

    I am wondering what is the reason this error message is showing up ..... 4 days ago the error message dion't show up.
    Are you running the script in a 64-bit PowerShell? The cmdlet Remove-LocalGroupMember isn't available in a 32-bit PowerShell:
    https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.localaccounts/?view=powershell-5.1

    Which version of Powershell are you running?

    ----------

    (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-09-05T15:56:08.613+00:00

    That's right I'm using x86.

    Now the error says:
    PS C:\Users\jose.fuentes> Remove-LocalGroupMember -Group "Administradores" -Member "pablo.roldan" Confirm:$false
    Remove-LocalGroupMember : No se encuentra ningún parámetro de posición que acepte el argumento 'Confirm:False'.
    En línea: 1 Carácter: 1
    + Remove-LocalGroupMember -Group "Administradores" -Member "pablo.rolda ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (:) [Remove-LocalGroupMember], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.RemoveLocalGroupMemberCommand

    PS C:\Users\jose.fuentes> Remove-LocalGroupMember -Group "Administradores" -Member "pablo.roldan" Confirm:$false  
    Remove-LocalGroupMember : No se encuentra ningún parámetro de posición que acepte el argumento 'Confirm:False'.  
    En línea: 1 Carácter: 1  
    + Remove-LocalGroupMember -Group "Administradores" -Member "pablo.rolda ...  
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
        + CategoryInfo          : InvalidArgument: (:) [Remove-LocalGroupMember], ParameterBindingException  
        + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.RemoveLocalGroupMemberCommand  
    

  3. FUENTES Jose 21 Reputation points
    2022-09-07T12:50:35.31+00:00

    No, other error

    PS C:\Users\jose.fuentes> Remove-LocalGroupMember -Group "Administradores" -Member "pablo.roldan" -Confirm:$false  
    Remove-LocalGroupMember : Acceso denegado.  
    En línea: 1 Carácter: 1  
    + Remove-LocalGroupMember -Group "Administradores" -Member "pablo.rolda ...  
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
        + CategoryInfo          : PermissionDenied: (Administradores:String) [Remove-LocalGroupMember], AccessDeniedException  
        + FullyQualifiedErrorId : AccessDenied,Microsoft.PowerShell.Commands.RemoveLocalGroupMemberCommand  
      
    
    0 comments No comments

  4. FUENTES Jose 21 Reputation points
    2022-09-22T10:49:46.923+00:00

    Any update, please??

    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.