You aren't writing anything to your log file. And you aren't doing any error checking (or logging). Try this:
$Days = 1280
$LastModified = (Get-Date).Adddays( - ($Days))
$Users = Get-ADUser -Filter { Enabled -eq $False } -SearchBase 'OU=DisabledAccount,DC=adr,DC=it' -Properties WhenChanged |
Where-Object { $_.WhenChanged -le (Get-Date).AddDays( - ($Days)) } |
Select-Object @{ Name = "Username"; Expression = { $_.SamAccountName } }, Name, WhenChanged, DistinguishedName
ForEach ($Item in $Users) {
$dn = $Item.DistinguishedName
Try{
Remove-ADUser -Identity $Item.DistinguishedName -Confirm:$false -ErrorAction Stop
}
Catch{
"DN = $dn was *NOT* removed`n`n$_" | Out-File -FilePath C:\Scripts\DeletedUsers.txt -Append
}
"DN = $DN WAS removed" |Out-File -FilePath C:\Scripts\DeletedUsers.txt -Append
}