I put together this script due to an ongoing AD account cleanup project and needed to put together a report of what was deleted from a particular date range.
*Will only work if the Active Directory Recycle Bin is enabled in your domain.*
write-host -ForegroundColor Cyan “Getting deleted AD objects from date specified”
$StartTime = read-host ” Enter start date in this format: 2/1/2015″
$EndTime = read-host “Enter end date in this format: 2/10/2015”
$Deleted = @()
$Deleted = Get-ADObject -Filter {(isdeleted -eq $true) -and (name -ne “Deleted Objects”)} -includeDeletedObjects -property whenChanged,LastKnownParent |
Where-Object {$_.whenChanged -ge $StartTime -and $_.whenChanged -le $EndTime} |
Select @{ Name=’UserName’; Expression={$_.Name}}, @{
Name=’LastLocation’; Expression={$_.LastKnownParent}}, @{
Name=’Deleted’;Expression={$_.whenChanged}} | sort whenChanged -Descending
$Deleted | out-gridview
Or, you can simply run this line below. Although, two interesting things to note here:
1. The information is truncated. And, setting the variable $FormatEnumerationLimit to =-1 doesn’t affect the truncated information.
2. Curious that it displays same day deletions except the above script. The above script is filtering for objects that have “isDeleted” attribute set to “True” and that object is found in “Deleted Objects’ container. The line below is using the “SearchBase” parameter and specifying the “Deleted Objects” DN path. Something might tweak to test this outcome.
Get-ADObject -SearchBase “CN=Deleted Objects,DC=hq,DC=crabel,DC=com” -Filter * -IncludeDeletedObjects -Properties LastKnownParent, whenChanged | Sort whenChanged -Descending | ft Name,whenChanged,LastKnownParent
Enjoy!