Skip to content

Instantly share code, notes, and snippets.

@julian-alarcon
Created December 4, 2017 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save julian-alarcon/f60d7e33fd286d64a82b983c894d783d to your computer and use it in GitHub Desktop.
Save julian-alarcon/f60d7e33fd286d64a82b983c894d783d to your computer and use it in GitHub Desktop.
PowerShell script to get and remove old computers not connected to AD
# Script to regenerate list and remove old computers not logged to Domain Controller in 2 Months
$time = (Get-Date).AddMonths(-2)
Get-ADComputer -Filter {LastLogonTimeStamp -lt $time} -Properties * | select name, distinguishedname,LastLogonDate| export-csv computers_2m.txt
Get-ADComputer -Filter {LastLogonTimeStamp -lt $time} -Properties distinguishedname| Remove-ADObject -Recursive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment