Cleaning Up AD

Here's one I wrote for it

<code> <# .DESCRIPTION This script will email a report of 60 day inactive users and computers. It is scheduled on DOT-SMP01 to run every Monday.

>

Connect-QADService -service 'dot.nc.net' Set-QADPSSnapinSettings -DefaultSizeLimit 0 $count = 0 $date = (Get-Date).adddays(-60) echo "Name Type Organizational Unit Modification Date Last Logon Date" > c:\temp\inactive.csv $compObjects = Get-QADComputer -LastChangedBefore $date | ?{$.ModificationDate -lt $date}| ?{$.WhenChanged -lt $date} Write-Host "." foreach ($obj in $compObjects) {$count++} echo "$count machines to test" foreach ($obj in $compObjects) { $name = Get-QADComputer $obj | %{$_.Name} $pingtest = ping $name -n 2 if ($pingtest -match "could not find host") {$result = $false} else {$result = $true} $count-- echo "Pinging $name, returned result of $result...$count machines left to test" if ($result -eq $false) { $type = $obj.type $ou = $obj.ParentContainerDN $mod = $obj.ModificationDate echo "$name $type $ou $mod" >> c:\temp\inactive.csv } }

$count = 0 $users = Get-QADUser -Enabled -LastChangedBefore $date | ?{$.AccountIsDisabled -eq $false} | ?{$.ModificationDate -lt $date} | ?{$.WhenChanged -lt $date} | ?{$.LastLogonTimeStamp -lt $date} foreach ($usr in $users) {$count++} echo "$count user meet the criteria, generating list." foreach ($usr in $users) { $uname = $usr.samaccountname $utype = $usr.type $uou = $usr.ParentContainerDN $umod = $usr.ModificationDate $ull = $usr.LastLogon echo "$uname $utype $uou $umod $ull" >> c:\temp\inactive.csv }

</code>

/r/sysadmin Thread