Friday, January 29, 2016

Find AD users not loged on 90 days

Count:
Find AD enabled users form OU and Group:

$lastLogon = (get-date).adddays(-90).ToFileTime()

(get-aduser -searchbase "OU=Test,DC=test,DC=com" -Server test.com -filter {memberof -recursivematch "CN=Groupname,OU=Groups,DC=test,DC=com" -and (lastLogonTimestamp -lt $lastLogon) -and (enabled -eq $true)}).count

Export:

$lastLogon = (get-date).adddays(-90).ToFileTime()

(get-aduser -searchbase "OU=Test,DC=test,DC=com" -Server test.com -filter {memberof -recursivematch "CN=Groupname,OU=Groups,DC=test,DC=com" -and (lastLogonTimestamp -lt $lastLogon) -and (enabled -eq $true)}))}) -Properties lastlogondate | select Name, UserPrincipalName | sort name

No comments: