Solved

PowerShell Script - List users that have not logged on in the past 3 months

Posted on 2016-10-17
3
47 Views
Last Modified: 2016-10-19
Does anyone have a PowerShell script that can list users in AD that have not logged on in the past 3 months?
0
Comment
Question by:amazon4life
  • 2
3 Comments
 
LVL 78

Expert Comment

by:David Johnson, CD, MVP
ID: 41847420
import-module ActiveDirectory
Search-ADAccount -AccountInactive -TimeSpan 90.00:00:00 | where {$_.ObjectClass -eq 'user'} | FT Name,ObjectClass –A

Open in new window

0
 
LVL 6

Accepted Solution

by:
Niten Kumar earned 500 total points
ID: 41847712
# List all users in that have not logged on within  
# XXX days in "Active Directory"  
#  
# This script requires the Active Directory Module from Microsoft.  
# It works with Server 2008 R2 and higher Domain Controllers
#
# Get the Current Date  
#  
$CurrentDate=GET-DATE  
#  
# Number of Days to check back.    
#  
$NumberDays=90  
#  
# Organizational Unit to search  
#  
$SearchBase='OU=Users,OU=Business,DC=Contoso,DC=Local'  
#  
Import-Module ActiveDirectory
GET-ADUSER -filter * -SearchBase $OU -properties LastLogonDate | where { $_.LastLogonDate.AddDays($NumberDays) -lt $CurrentDate } | Format-Table
0
 
LVL 6

Expert Comment

by:Niten Kumar
ID: 41847723
You can also try the following script:

Search-ADAccount -AccountInactive -TimeSpan 90.00:00:00 | ?{$_.enabled -eq $true} | %{Get-ADUser $_.ObjectGuid} | select name, givenname, surname | export-csv c:\report\unusedaccounts.csv -NoTypeInformation
1

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Find out how to use Active Directory data for email signature management in Microsoft Exchange and Office 365.
A brief introduction to what I consider to be the best editor for PowerShell.
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now