Hi,
Please see if this works for you.
$searchbase = "OU=Users Without Machine _ Vendor,DC=Clix,DC=local"
$Days = (Get-Date).AddDays(-30)
$DCs = Get-ADDomainController -Filter *
$users=foreach($user in (Get-ADUser -Filter {Enabled -eq $True} -SearchBase $searchbase)){
$DCs | ForEach-Object { Get-ADUser $user -Server $_ -Properties lastLogOn,LogonCount } | Sort-Object -Property lastLogOn | Select-Object -Last 1 |
Where-Object {[datetime]::FromFileTime($_.LastLogon) -lt $Days -and $_.DistinguishedName -notlike "*DisabledUsers*" -and $_.DistinguishedName -notlike "*ServiceAccount*" -and $_.LogonCount -gt "0"}
}
Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.