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-08T12:19:06.293+00:00

    Please try this:

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

    ----------

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

    Regards
    Andreas Baumgarten


  2. FUENTES Jose 21 Reputation points
    2022-08-10T09:27:08.913+00:00
    PS C:\Users\jose.fuentes> Import-Csv -path "C:\Temp\Files\Users.csv" | ForEach-Object {Remove-localgroupmember -Group "Administradores" -Member $_.'Account' -Confirm:$false}  
    Remove-localgroupmember : El término 'Remove-localgroupmember' no se reconoce como nombre de un cmdlet, función, archivo de script o programa   
    ejecutable. Compruebe si escribió correctamente el nombre o, si incluyó una ruta de acceso, compruebe que dicha ruta es correcta e inténtelo de nuevo.  
    En línea: 1 Carácter: 62  
    + ... mp\Files\Users.csv" | ForEach-Object {Remove-localgroupmember -Group  ...  
    +                                           ~~~~~~~~~~~~~~~~~~~~~~~  
        + CategoryInfo          : ObjectNotFound: (Remove-localgroupmember:String) [], CommandNotFoundException  
        + FullyQualifiedErrorId : CommandNotFoundException  
       
    Remove-localgroupmember : El término 'Remove-localgroupmember' no se reconoce como nombre de un cmdlet, función, archivo de script o programa   
    ejecutable. Compruebe si escribió correctamente el nombre o, si incluyó una ruta de acceso, compruebe que dicha ruta es correcta e inténtelo de nuevo.  
    En línea: 1 Carácter: 62  
    + ... mp\Files\Users.csv" | ForEach-Object {Remove-localgroupmember -Group  ...  
    +                                           ~~~~~~~~~~~~~~~~~~~~~~~  
        + CategoryInfo          : ObjectNotFound: (Remove-localgroupmember:String) [], CommandNotFoundException  
        + FullyQualifiedErrorId : CommandNotFoundException  
    
    0 comments No comments

  3. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2022-08-10T11:36:39.097+00:00

    Hi @FUENTES Jose ,

    please run just this one line for testing:

    Remove-LocalGroupMember -Group "Administradores" -Member "pablo.roldan" Confirm:$false  
    

    Is this a different computer you are running the script?
    Before the error message doesn't show up about a missing cmdlet (if I translate the error message right / the english error message would be more helpful).

    ----------

    (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

  4. FUENTES Jose 21 Reputation points
    2022-08-12T07:09:09.95+00:00
    Remove-LocalGroupMember : El término 'Remove-LocalGroupMember' no se reconoce como nombre de un cmdlet, función, archivo de script o programa   
    ejecutable. Compruebe si escribió correctamente el nombre o, si incluyó una ruta de acceso, compruebe que dicha ruta es correcta e inténtelo de nuevo.  
    En línea: 1 Carácter: 1  
    + Remove-LocalGroupMember -Group "Administradores" -Member "pablo.rolda ...  
    + ~~~~~~~~~~~~~~~~~~~~~~~  
        + CategoryInfo          : ObjectNotFound: (Remove-LocalGroupMember:String) [], CommandNotFoundException  
        + FullyQualifiedErrorId : CommandNotFoundException  
    

    It's always the same computer.

    The error said more or less
    no se reconoce como nombre de un cmdlet, función, archivo de script o programa
    ejecutable. Compruebe si escribió correctamente el nombre o, si incluyó una ruta de acceso, compruebe que dicha ruta es correcta e inténtelo de nuevo.
    En línea: 1 Carácter: 1
    is not recognized as the name of a cmdlet, function, script file or executable program.
    executable program. Check if you typed the name correctly or, if you included a path, check that the path is correct and try again.
    Inline: 1 Character: 1

    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.