Skip to content

Instantly share code, notes, and snippets.

@tech-zombie
Last active May 22, 2017 11:25
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 tech-zombie/4a35d95f5dd64187eca6f40ac4919fa1 to your computer and use it in GitHub Desktop.
Save tech-zombie/4a35d95f5dd64187eca6f40ac4919fa1 to your computer and use it in GitHub Desktop.
polls ad for computer objects that have not called home for a period of time, can either report or delete them.
import-module activedirectory
#finddomain or enter manually
#$domain = (Get-WmiObject Win32_ComputerSystem).Domain
#domain = my.domain.com
#edit for number of days to go back
$DaysInactive = 90
$time = (Get-Date).Adddays(-($DaysInactive))
# Get all AD computers with lastLogonTimestamp less than our time
Get-ADComputer -Filter {LastLogonTimeStamp -lt $time} -Properties LastLogonTimeStamp |
# Output hostname and lastLogonTimestamp into CSV
#select-object Name,@{Name="Stamp"; Expression={[DateTime]::FromFileTime($_.lastLogonTimestamp)}} | export-csv OLD_Computer.csv -notypeinformation
#delete AD objects
#remove-adcomputer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment