May 31 2021 07:38 AM - edited May 31 2021 11:05 PM
Hi,
I triyng to clean my AD from old computer objects that not logged on more then 90 days.
I run this script that i found in the network:
Import-Module ActiveDirectory
# Set the number of days since last logon
$DaysInactive = 90
$InactiveDate = (Get-Date).Adddays(-($DaysInactive))
#-------------------------------
# FIND INACTIVE COMPUTERS
#-------------------------------
# Automated way (includes never logged on computers)
$Computers = Search-ADAccount -AccountInactive -DateTime $InactiveDate -ComputersOnly | Select-Object Name, LastLogonDate, Enabled, DistinguishedName
#-------------------------------
# REPORTING
#-------------------------------
# Export results to CSV
$Computers | Export-Csv C:\Temp\InactiveComputers.csv -NoTypeInformation
#-------------------------------
# INACTIVE COMPUTER MANAGEMENT
#-------------------------------
# Below are two options to manage the inactive computers that have been found. Either disable them, or delete them. Select the option that is most appropriate for your requirements:
# Disable Inactive Computers
ForEach ($Item in $Computers){
$DistName = $Item.DistinguishedName
Set-ADComputer -Identity $DistName -Enabled $false
Get-ADComputer -Filter { DistinguishedName -eq $DistName } | Select-Object Name, Enabled
}
# Delete Inactive Computers
ForEach ($Item in $Computers){
Remove-ADComputer -Identity $Item.DistinguishedName -Confirm:$false
Write-Output "$($Item.Name) - Deleted"
}
My question I tried a few times without success.
1)I need to check old accounts on all the AD and Exclude 1 OU.
2) I want disable the accounts and delete them after 180 days
Thanks for the help
Jun 03 2021 12:28 AM
SolutionHi
To get all the object excluding 1 OU you need to use the where and filter base on the OU name.
For example, lets assume The OU you want to exclude is "OU=Computers,OU=OU1,DC=local"
The search for computer
$Computers = Search-ADAccount -AccountInactive -DateTime $InactiveDate -ComputersOnly | where {$_.DistinguishedName -notlike "*OU=Computers,OU=OU1,DC=local"} | Select-Object Name, LastLogonDate, Enabled, DistinguishedName
for the deletion, you need to have another script to do the deletion or use parameters for the script to tell that the operation is for deletion
------------
If this answer helps, Please give a like and click on Best Respone.
Jun 06 2021 01:19 AM
Jun 03 2021 12:28 AM
SolutionHi
To get all the object excluding 1 OU you need to use the where and filter base on the OU name.
For example, lets assume The OU you want to exclude is "OU=Computers,OU=OU1,DC=local"
The search for computer
$Computers = Search-ADAccount -AccountInactive -DateTime $InactiveDate -ComputersOnly | where {$_.DistinguishedName -notlike "*OU=Computers,OU=OU1,DC=local"} | Select-Object Name, LastLogonDate, Enabled, DistinguishedName
for the deletion, you need to have another script to do the deletion or use parameters for the script to tell that the operation is for deletion
------------
If this answer helps, Please give a like and click on Best Respone.