PoshCode Archive  Artifact Content

Artifact 26c391e9f167362e2f12f8ffc41d961cb2df53876f21a2b399b2926486942dba:

  • File Remove-MyOldComputers.ps1 — part of check-in [4fd34c14d8] at 2018-06-10 12:56:34 on branch trunk — Removes AD objects matching the filter (Name=$Name*) older than $MaxDaysOld (user: unknown size: 1001)

# 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()
  }
}