For a single server it is working fine. It is removing the server ANA-SRV002 & few security groups those are contain “ANA-SRV002” like “Printer_GRP_ ANA-SRV002_DP” but below script is not giving any error neither removing any object.
#It is working fine
Get-ADObject -Filter { CN -like “ANA-SRV002” } | Remove-ADObject -Confirm:$true
#Problemetic one for bulk servers
$Servers = Get-Content D:\Biswajit-Temp\Scripts\Decom_SRVs.txt
$ErrorActionPreference = “Stop”
foreach ($Server in $Servers)
{
Try {
Get-ADObject -Filter { CN -like “$Server” } | Remove-ADObject -Confirm:$true
}
Catch
{
Add-Content “$Server Object not found” -path $env:USERPROFILE\Desktop\Decom_UnknownHosts.txt
}
}