I'm trying to create a script that will show me all of the AD accounts that have been expired for more than 30 days. So, I did this:
Import-Module ActiveDirectory
$then = (Get-Date).AddDays(-30)
Get-ADUser -Properties Name,Title,SAMAccountName,accountExpires,AccountExpirationDate,homeDirectory -Filter {accountExpires -lt $then}
Doing this pulls up 56 users, however some of these users were expired and reinstated, so I need to filter on the 'accountexpires' property to see only those accounts currently expired:
Import-Module ActiveDirectory
$then = (Get-Date).AddDays(-30)
Get-ADUser -Properties Name,Title,SAMAccountName,accountExpires,AccountExpirationDate,homeDirectory -Filter {accountExpires -lt $then -AND accountExpires -ne "0"}
Doing this pulls up 36 users, which is exactly correct. Now I want to exclude those users that are expired due to military leave. There are only 2 users in this list that match that criteria so it should show me 34 accounts. However, when I run the following script it only returns 3:
Import-Module ActiveDirectory
$then = (Get-Date).AddDays(-30)
Get-ADUser -Properties Name,Title,SAMAccountName,accountExpires,AccountExpirationDate,homeDirectory -Filter {accountExpires -lt $then -AND accountExpires -ne "0" -AND description -ne "Inactive Military Leave"}
So, what am I doing wrong?
Your help is appreciated! Thanks!