PowerShell: Removing a list of computers from Active Directory

@Kid_Zer0 on Twitter recently asked the following question:

Need to delete a list of computers from AD – anyone know how to do this in #PowerShell or #VBScript (List is from a file)

I’ve previously written several versions of an Active Directory cleanup script, but if you’re not seeking something that complicated, you can simply leverage the cmdlets built into Microsoft Windows Server 2008 R2. This simple, one-liner command should take a list of computers from a text file, and remove them from Active Directory:

Get-Content ComputersToDelete.txt | % { Get-ADComputer -Filter { Name -eq $_ } } | Remove-ADComputer -WhatIf

Upon running this command, you should receive output similar to the following (my text file only has one computer account in it):

If you want the script to actually make changes, simply leave the –WhatIf off the end of the script. Keep in mind that this command also depends on the input text file to be in the local directory, otherwise you will need to specify the full path to the text file. Another assumption in this command, is that you are running the command from a server that is a member of the same domain that you want to remove accounts from. If you have a more complex Active Directory environment, you will need to use other parameters to specify the domain / forest, and credentials if necessary, that you need to act against.

Hope this helps!