# encoding: ascii
# api: powershell
# title: Remove-MyOldComputers
# description: Removes AD objects matching the filter (Name=$Name*) older than $MaxDaysOld
# version: 0.1
# license: CC0
# x-poshcode-id: 1162
# x-archived: 2009-06-27T20:29:11
#
#
##
# Remove-MyOldComputers.ps1
#
#  Makes certian assumptions about your computers naming scheme: mainly that all your
#  computers are named as follows: {username}* I.E. bob1, bob-test, bob-server, etc
#
##
param (
  [String] $Name=((whoami).Split('\')[1]),
  [Int32] $MaxDaysOld=20
)


$root = [ADSI]''
$searcher = new-object System.DirectoryServices.DirectorySearcher($root)
$searcher.filter = "(Name=$Name*)"
$computers = $searcher.findall()
$computers | % {
  $Comp = [ADSI]$_.Path  
  $LastChange = [DateTime]::Now - [DateTime][String]$Comp.WhenChanged
  if ($LastChange.TotalDays -gt $MaxDaysOld) {
    Write-Host Deleting $Comp.Name [$LastChangeTimeSpan.TotalDays days old]
    $Comp.psbase.DeleteTree()
  }
}
