3

I am trying to list all accounts that have not been logged into outside of 6 months.

This is my first time really using powershell and I am simply editing other people's scripts at this point to fit my own needs.

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

Code for computers outside six months.

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

Code for users outside six months.

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

However, these are not working and are just spitting out all accounts. I have also noticed changing the -6 to any number really has no effect. Suggestions?

JPBlanc
  • 70,406
  • 17
  • 130
  • 175
Zac Borders
  • 137
  • 1
  • 3
  • 12

4 Answers4

2

Have a look here for Tracking the inactive users by categories(logon status,disabled users,password expired users,acc expired users,neverloggedon users,deleted users,etc)

http://www.adsysnet.com/asn-active-directory-inactive-account-tracker-features.aspx

Stephen
  • 21
  • 2
1

Your test is OK (it's working in my AD) the only thing is that you have to eliminate the objects where $_.lastlogondate is null.

try :

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

Edited :

Because the lastLogon attribute is not replicated in Active Directory, a different value can be stored in the copy of Active Directory on each Domain Controller. One solution is to loop over all domains controlers to build a list such users. But I'am quite sure there is an other solution !

JPBlanc
  • 70,406
  • 17
  • 130
  • 175
0

I believe you need to pass a date or a timespan along with the -AccountInactive switch. Doing should make your custom 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
goric
  • 11,491
  • 7
  • 53
  • 69
  • I am having the same issue with your script as mine. I can change the 6 to anything and still doesn't affect the script. And in the list it is giving me computers/users that I know for a fact are used on a daily basis. – Zac Borders May 03 '12 at 20:09
  • Try with a hardcoded date, omitting the -useronly parameter, and without the filter, just to see what happens? `Search-ADAccount -accountinactive -datetime "11/3/2011"` – goric May 03 '12 at 20:19
0
$sixMonths = (Get-Date).AddMonths(-6)
Search-ADAccount -accountinactive -usersonly -datetime "$sixMonths"
David Brabant
  • 41,623
  • 16
  • 83
  • 111