Contents

PS | List Inactive AD Users and Computers for Past X Days

You are here:
← All Topics

Steps to list inactive AD users and computers for past X days with powershell

List Inactive AD Users

To list all users who had not login to AD for past 30 days

#User not login for past 30 days
Import-Module ActiveDirectory

$Users = Get-ADUser -Filter * -Properties * | Select SamAccountName, LastLogonDate
$DaysInActive = "30" #Inactive for 30 Days
$ReportPath = "c:\temp\InActiveUsers.csv" #Report Path

$TotalUsers = @()

$Count = 0
Foreach ($user in $Users) {

    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 -Path $ReportPath -NoTypeInformation

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

Output

Refer to C:\temp\InActiveUsers.csv for more detail information

u002 had not login for past 30 days
MeganB had not login for past 30 days
30 Users had not login for past 30 days

List Inactive Computers

To list all computers who had not login to AD for past 30 Days

#Inactive Computer Objects for past 30 days
Import-Module ActiveDirectory
$Computers = Get-ADComputer -Filter * -Properties SamAccountName, PasswordLastSet | Select SamAccountName, PasswordLastSet
$DaysInActive = "30"
$ReportPath = "c:\temp\Computers.csv" #Report Path
$Count = 0

foreach ($Computer in $Computers) {
 
    if ($Computer.PasswordLastSet -lt (Get-Date).AddDays( - ($DaysInActive))) {
        write-host $Computer.SamAccountNAme "had not login for past" $DaysInActive "days"
        $Count++
        $TotalComputers += @($Computer) #Insert all the objects into array
    }
 
}

$TotalComputers | export-csv $ReportPath -NoTypeInformation
Write-Host $Count "computers had not login for past" $DaysInActive "days"

Output

Refer to C:\temp\computers.csv for more detail information

INFO-SQLCLS$ had not login for past 60 days
INFO-SQLAO$ had not login for past 60 days
4 computers had not login for past 60 days

List Users who had not login to Exchange for past X Days

To list users who had not login to Exchange Server for past 30 days

#Connect to Exchange - PowerShell ISE - Exchange 2013 and above 
. 'C:\Program Files\Microsoft\Exchange Server\V15\bin\RemoteExchange.ps1'
Connect-ExchangeServer -auto

$AllMailbox = Get-Mailbox -RecipientTypeDetails UserMailbox
$DaysInActive = "30" #Inactive for 30 Days
$ReportPath = "c:\Temp\ExchangeReport.csv" #Report Path
$TotalUsers = @()
$Count = 0

#Inlcude UPN
foreach ($Mailbox in $AllMailbox) {

    $UPN = $Mailbox.UserPrincipalName
    $TempUser = MailboxStatistics -Identity $UPN | Select DisplayName, @{N = "UPN" ; E = { $UPN } }, LastLogonTime
    $TempUsers += @($TempUser)

}

foreach ($User in $TempUsers) {

    if ($user.lastLogonTime -lt (Get-Date).AddDays( - ($DaysInActive))) {
        
        Write-host $user.UPN "had not login for past 30 days"
        $Count++
        $TotalUsers += @($user) #Insert all the objects into array
    }
}
$TotalUsers | Export-Csv -Path $ReportPath -NoTypeInformation
Write-Host $Count "Users had not login for past $DaysInActive days"

Output

Refer to C:\temp\ExchangeReport.csv for more detail information

[email protected] had not login for past 30 days
1 Users had not login for past 30 days

Top Posts & Pages

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top