PowerShell: Find Inactive Users in AD

Please refer to the following PowerShell to find the inactive users (Users who do not login to AD Domain) for x numbers of days  in Active Directory Domain Controller


#User not login for past 30 days
import-module ActiveDirectory

$ADUser=get-aduser -filter * -properties * | Select SamAccountName, LastLogonDate
#You can sepecify the number of days here
$DaysInActive = 30
$ReportPath = "c:\temp\Users.csv" #Report Path
$Count=0
foreach ($user in $ADUser) {

if ($user.lastLogonDate -lt (Get-Date).AddDays(-($DaysInActive))){

write-host $user.SamAccountNAme "had not login for past 30 days"
$Count++
$TotalUsers +=@($user) #Insert all the objects into array

}

}
$TotalUsers | export-csv $ReportPath -NoTypeInformation
#Show how many users had not been login for past x days
Write-Host $Count "User had not login for past $DaysInActive days"

With the export csv file, you can do some filtering and sorting in Excel to get the following report

PowerShell-FindInActiveUsers-01.jpg

 

 

 

Share This