Hi all,
Hoping someone would be able to give me some guidance here. I'm using the below script to automatically disable any computer accounts that have been inactive for 30 days and move it to a disabled computers OU.
I'm targetting our root computers folder which has multiple OUs with computer accounts in them. How could I exclude a sub OU from the below search? Thanks!
Get-ADComputer -SearchBase "OU=Computers - ALL,DC=example,DC=com" -Properties Name,lastLogonDate,CanonicalName -Filter {lastLogonDate -lt $days} | Where-Object {$_.Enabled
-eq $true} | ? {$_.DistinguishedName -like "*OU=Computers - ALL*"} | Set-ADComputer -Enabled $false
Start-Sleep -s 30
Get-ADComputer -SearchBase "OU=Computers - ALL,DC=example,DC=com" -Properties Name,lastLogonDate,CanonicalName -Filter {lastLogonDate -lt $days} | Where-Object {$_.Enabled -eq
$false} | ? {$_.DistinguishedName -like "*OU=Computers - ALL,*"} | Move-ADObject -TargetPath 'OU=Computers - Disabled,DC=example,DC=com'