none
Delete Multiple computer listed in txt file from Active directory using Powershell. RRS feed

  • Question

  • I need to Delete Multiple computer listed in txt file from Active directory using PowerShell.

    The file has name of computer only eg. ACL014111

    Any suggestion will be appreciated.

    Wednesday, June 19, 2019 7:10 PM

All replies

  • Hello Rag01,

    You could try to convert the .txt to a csv file and use something like the following:

    $Computers = Import-Csv -Path C:\Temp\computers.csv
    foreach ($Computer in $Computers) {
        Remove-ADComputer -Identity $Computer
    }

    • Proposed as answer by Leo Erlandsson Thursday, June 20, 2019 5:37 AM
    Wednesday, June 19, 2019 10:10 PM
  • The computer objects have child properties with them (Bit locker keys associated) so I cant use  Remove-ADComputer -Identity. I tried using this instead(Remove-ADObject-Identity-Recursive) It does not delete the computer object. If I use this without the loop of reading the computer name from file, it works.

    Get-ADObject -Filter { name -eq "acl016118" } | Remove-ADObject -Recursive

     

    Thursday, June 20, 2019 4:47 PM
  • Hi Rag,

    How about the following than:

    $Computers = Import-Csv -Path C:\Temp\computers.csv
    foreach ($Computer in $Computers) {
        Get-ADObject | Where-Object {$_.Name -eq $Computer  } | Remove-ADComputer -Recursive
    }

    Thursday, June 20, 2019 6:38 PM
  • Thank for so much for looking into this.

    I have the csv file with just two computer name - eg. BDL012111,ACL014115

    The script is below - but still the object does not get deleted.

    Import-ModuleActiveDirectory


    $Computers

    =Import-Csv-PathC:\IT_Stuff\mycomputers.csv


    foreach($Computerin$Computers) {

        

    Get-ADObject|Where-Object{$_.Name -eq$Computer   } |Remove-ADComputer-Recursive


    }


    • Edited by Rag01 Thursday, June 20, 2019 7:39 PM
    Thursday, June 20, 2019 7:38 PM
  • 2 Issues

    1.  Your looping script isn't providing a filter for the get-adobject command and you might want to use Get-ADComputer instead so that you only return computer objects
    2.  Your CSV file might not be formatted properly


    Import-ModuleActiveDirectory
    
    $Computers = Import-Csv -Path C:\IT_Stuff\mycomputers.csv
    
    foreach($Computer in $Computers) {
    
    Get-ADComputer $Computer.Name |Remove-ADComputer -Recursive
    
    } 

    In the csv file are the computers on different lines or do you have them on the same line separated by commas?

    What you want to do is have them on separate lines so it should look like this:

    Name
    BDL012111
    ACL014115

    A good test is to do a Write-Verbose 


    David Lundell, Twitter | Hire Identity Managed | FIM Best Practices book | How to Be an MVP in Life book

    Wednesday, June 26, 2019 1:51 AM