Powershell Inactive Account List

I am trying to list all accounts that are not logged in beyond 6 months.

This is my first time that I really use powershell, and I'm just editing other people's scripts at this point to fit my own needs.

I want to divide the search into two lists: only computers and only users.

Code for computers in six months.

Search-ADAccount -accountinactive -computersonly | where {$_.lastlogondate -lt (get-date).addmonths(-6)} | FT Name,LastLogonDate

Code for users over six months old.

Search-ADAccount -accountinactive -usersonly | where {$_.lastlogondate -lt (get-date).addmonths(-6)} | FT Name,LastLogonDate

However, they do not work and just spit out all the accounts. I also noticed that a change of -6 does not really affect any number. Suggestions?

+3
source share
4 answers

( AD), , , $_. lastlogondate null.

:

Search-ADAccount -accountinactive -usersonly  | where {! ($_.lastlogondate -lt (get-date).addMonths(-6))} | ft Name,lastlogondate

:

lastLogon Active Directory, Active Directory . . , !

+1

I believe you need to pass a date or time using the -AccountInactive switch. Execution should make your own filter unnecessary, so try something like this (untested):

Search-ADAccount -accountinactive -datetime (get-date).AddMonths(-6) -computersonly | ft Name,LastLogonDate
Search-ADAccount -accountinactive -datetime (get-date).AddMonths(-6) -userssonly | ft Name,LastLogonDate
0
source
$sixMonths = (Get-Date).AddMonths(-6)
Search-ADAccount -accountinactive -usersonly -datetime "$sixMonths"
0
source

All Articles