Avatar of Rammy Charles
Rammy Charles
Flag for United States of America asked on

PS script for report of accounts havent logged in for a long time

Is there a powershell script that can run a report against active directory to seek and find accounts who havent logged into the forest in a long time?
PowershellActive Directory

Avatar of undefined
Last Comment
Jeremy Weisinger

8/22/2022 - Mon

You can check last long time for user using PS. Check this Script:

Test it.
Shaun Vermaak

austin minor

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Rammy Charles

austin that is what i wanted. do you oknow if i can add filters to not show some accounts on the report and also how can i sort by lastlogontimestamp?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Jeremy Weisinger

You filter the results by using Where-Object (aka ?). So if you have a list of users you could match the current name against it.
And sorting is easy enough. Sort-Object <property name>.  Putting it together is something like this:
$userfilter = Get-Content C:\filterlist.txt
Search-ADAccount -UsersOnly -AccountInactive -TimeSpan 90 | ?{$_.enabled -eq $True} | Get-ADUser -Properties Name, EmailAddress, Department, Description, lastLogonTimestamp | ?{$userfilter -notmatch $_.SamAccountName} | Select Name, EmailAddress, Department, Description,@{n='lastLogonTimestamp';e={[DateTime]::FromFileTime($_.lastLogonTimestamp)}} | sort lastlogontimestamp | Export-Csv D:\temp\testfunytest.csv

Open in new window

Note the additions of
$userfilter = Get-Content C:\filterlist.txt
?{$userfilter -notmatch $_.SamAccountName}
sort lastlogontimestamp

Be sure to edit for your environment.