• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

danblee.com

Tutorials & Knowledge Base Articles for System Administrators who wear many, many hats.

  • Home
  • About
  • Ask Me
  • DBLHost.com

PowerShell script to remove all Active Directory computers in a list

May 16, 2017 by Dan B. Lee Leave a Comment

Scenario

You made an awesome list of old computers and now you want to delete these computers using PowerShell for the sake of cleaning up AD.

The Script

Here you go:

Import-Module ActiveDirectory

Get-Content C:\scripts\to-delete.txt | % { Get-ADComputer -Filter { Name -eq $_ } } | Remove-ADObject -Recursive -WhatIf

Explanation

Separate each action by the pipe, that’s the little thing that looks like |

Get-Content is where you’ll grab the information in the text file. Mine was located at C:\scripts\to-delete.txt but you can put it anywhere as long as you write out the whole path.

The Get-ADComputer -Filter is comparing the computers that are there to folders that are in your list. Don’t worry about the syntax, just trust it.

Finally, remove the ADObject using Remove-ADObject

Remove-ADObject instead of Remove-ADComputer?!

Yes. This is because it performs the same result as Remove-ADComputer but with Remove-ADObject you can add -Recrusive. Why!? Because some objects in AD have sub objects. For example, some of the computers in my list had an OU underneath it. You’ll get a nasty message if you don’t do this. It looks like this:

Remove-ADComputer : The directory service can perform the requested operation only on a leaf objectAt line:x

What’s the -WhatIf

You don’t just want to delete anything without testing your work. What if there was a typo and this script deleted all your machines? Put the -WhatIf at the end of it and you’ll get a great read out of what’s about to happen:

It didn’t do this action, it just showed you what it would do if you removed the -WhatIf attribute.

Filed Under: Active Directory, Microsoft Windows, PowerShell Tagged With: AD, Cleanup, PowerShell, WhatIf

Primary Sidebar

Categories

  • Active Directory
    • Group Policy
  • Adobe Photoshop
  • Browsers
    • Chrome
  • Cloud Based Technology
    • Citrix XenApp
  • ConnectWise
    • LabTech
  • Dell
  • Internet Service Providers (ISPs)
  • iPhone
  • Linux
    • CentOS
    • OpenVPN
    • SaltStack
    • Ubuntu
  • Microsoft Office
    • Lync
    • Microsoft Excel
    • Microsoft Outlook
    • Microsoft Word
  • Microsoft Windows
    • Active Directory
    • PowerShell
    • Windows 7
    • Windows 8 Preview & Release
  • Networks
  • News
  • Off Topic
  • Office 365
  • Peripherals
    • Monitors
  • Printers
    • Local Printers
    • Network Printers
  • Programming
    • Python
  • Riverbed
  • Security
  • Sharepoint
  • Social Media
  • Splunk
  • Storage
  • Switching & Routing
    • Cisco
    • Fortinet
  • Technology Culture
    • Opinion
  • Uncategorized
  • Virtualization
    • VEEAM
    • VMware
  • WAMP
  • Web Hosting
    • WHMCS
  • Website Design
  • Windows Server
    • Exchange 2003
    • Exchange 2010
    • Exchange 2013
    • Microsoft SQL
    • Windows Server 2003
    • Windows Server 2008 R2
    • Windows Server 2012
  • WordPress

Footer

Recent Activity

  • pings on Xerox Phaser 3635MFP Default Admin Username and Password
  • Xerox Phaser 3635MFP Default Admin Username and Password — danblee.com on About Dan B. Lee
  • Estudio Login | LOGINEGG on Toshiba E-Studio Default Administrator Username and Password
  • Estudio Login | LOGINSPENT on Toshiba E-Studio Default Administrator Username and Password
  • Estudio Login | CHARTLOGIN on Toshiba E-Studio Default Administrator Username and Password

Dan Lee

Dan B. Lee works at SyApps, LLC., a Managed Hosting Solutions Firm, as a Senior Network Engineer. Dan has a decade of IT experience and specializes in a number of different disciplines including Virtualization, Web Site Hosting and Design, Network Security, Data Center Architecture, Local and Remote Server Hosting, and Backup & DRS Solutions. Read More…

Links

  • Home
  • About Dan B. Lee
  • Ask Me
  • Privacy Policy

Copyright © 2021 · Genesis Child Theme on Genesis Framework · WordPress · Log in