Hey folks!
Here is a quick way to find inactive AD Users in your environment. Get-ADUser ist the cmdlet we are going to use.
We are getting all users from the highest OU (domain.com) and using the Property LastLogonDate, which will not be returned if not specified in the -Properties parameter. After that a Where statement is going to show users that haven’t logged in since 90 days or more.
Get-ADUser -Filter * -Properties lastlogondate | where { $_.lastlogondate -lt (Get-Date).AddDays(-90) }
We could also specify the OU where the command is going to search:
Get-ADUser -Filter * -Properties lastlogondate -SearchBase “OU=TerminatedEmployees,DC=Company,DC=com”| where { $_.lastlogondate -lt (Get-Date).AddDays(-90) }|select Name,Lastlogondate
There is also the -SearchScope Onelevel parameter to determine that we are not going to search recursively:
Get-ADUser -Filter * -Properties lastlogondate -SearchBase “OU=TerminatedEmployees,DC=Company, DC=com” -SearchScope OneLevel | where { $_.lastlogondate -lt (Get-Date).AddDays(-90) }|select Name,Lastlogondate
Have fun!