Trevor Sullivan's Tech Room

Minding the gap between administration and development

PowerShell: Removing a list of computers from Active Directory

Posted by Trevor Sullivan on 2010/08/27

@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!


One Response to “PowerShell: Removing a list of computers from Active Directory”

  1. David said

    Is there a way to export the values returned when running the -whatif into a spreadsheet or any other file format?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: