Powershell to fine inactive accounts

I am trying to setup a powershell to find accounts that have been inactive 90 days or longer. I found the one below, but when compared against the LastLogonDate, several of the accounts have been logged in within the 90 day period.

Search-ADAccount -UsersOnly -AccountInactive -TimeSpan 90 | Get-ADUser -Properities Name, sAMAccountName, LastLogonDate | Where {($_.userAccountControl -band 2) -eq $False} | Select Name, sAMAccountName, LastLogonDate

How can I get a correct listing of users that have not been active 90 days or longer?
jjwolvenAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David Johnson, CD, MVPOwnerCommented:
import-module activedirectory 
$domain = "example.com" 
$DaysInactive = 90 
$time = (Get-Date).Adddays(-($DaysInactive))
 # Get all AD User with lastLogonTimestamp less than our time and set to enable
Get-ADUser -Filter {LastLogonTimeStamp -lt $time -and enabled -eq $true} -Properties LastLogonTimeStamp |
select-object Name,@{Name="Stamp"; Expression={[DateTime]::FromFileTime($_.lastLogonTimestamp)}} |
 export-csv OLD_User.csv -notypeinformation

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Tom CieslikIT EngineerCommented:
Replace domain name and path for export

import-module activedirectory  
$domain = "domain.mydom.com"  
$DaysInactive = 90  
$time = (Get-Date).Adddays(-($DaysInactive)) 
  
# Get all AD User with lastLogonTimestamp less than our time and set to enable 
Get-ADUser -Filter {LastLogonTimeStamp -lt $time -and enabled -eq $true} -Properties LastLogonTimeStamp | 
  
# Output Name and lastLogonTimestamp into CSV  
select-object Name,@{Name="Stamp"; Expression={[DateTime]::FromFileTime($_.lastLogonTimestamp).ToString('yyyy-MM-dd_hh:mm:ss')}} | export-csv c:\temp\OLD_User.csv -notypeinformation

Open in new window

0
Shaun VermaakTechnical SpecialistCommented:
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Ajit SinghCommented:
Something like:

Search-ADAccount -UsersOnly -AccountInactive -TimeSpan 30 | ?{$_.enabled -eq $True} | Get-ADUser -Properties Name, EmailAddress, Department, Description, lastLogonTimestamp | Select Name, EmailAddress, Department, Description,@{n='lastLogonTimestamp';e={[DateTime]::FromFileTime($_.lastLogonTimestamp)}} | Export-Csv D:\temp\testfunytest.csv

Open in new window


https://www.experts-exchange.com/questions/29064401/Disable-a-User-account.html

https://www.experts-exchange.com/questions/29033302/How-do-you-keep-your-AD-clean-of-old-stale-Computer-records.html

Hope this helps!
0
jjwolvenAuthor Commented:
Testing now.

The one from Ajit gave me results that had entries that had logged in within the TimeSpan.
Shaun's cleanup tool would do too much automatically; we have other plans for accounts.

Tom and David had the same script, which I will be testing next.
0
jjwolvenAuthor Commented:
It is working now, thanks.
I almost forgot to take into account that your script removed the inactive ones.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.