IAMinerva
HomeBlogAbout
m3M365 NewscoMicrosoft CopilotteMicrosoft TeamsshSharePoint & OneDriveinIntune & SecurityexExchange & OutlookpoPower PlatformazAzure & Entra IDtuTutorials & GuidesevEvents & ConferencesseSecuritywiWindows
IAMinerva

Professional blog dedicated to the Microsoft 365 ecosystem.

Quick links

HomeBlogAboutNewsletter

Stay informed

Get the latest Microsoft 365 news delivered straight to your inbox.

© 2026 IAMinerva. All rights reserved.

Built withNext.js&Tailwind
Identifier les utilisateurs inactifs Active Directory avec PowerShell
BlogWindowsIdentify Inactive Active Directory Users with PowerShell
Windows#Active Directory#PowerShell#Inactive Users

Identify Inactive Active Directory Users with PowerShell

Detect inactive Active Directory users with PowerShell. Automated script for inactivity audit, OU filtering and CSV report generation.

Houssem MAKHLOUF
June 25, 2026
7 min read

TL;DR par Minerva

généré par IA

Detect inactive Active Directory users with PowerShell. Automated script for inactivity audit, OU filtering and CSV report generation.

Introduction

Inactive user accounts in Active Directory represent far more than simple administrative disorder. They constitute a major security vulnerability, complicate access audits and create unnecessary burden on directory infrastructure. Former employees, departed contractors and abandoned project accounts accumulate gradually, forming fertile ground for security breaches and regulatory non-compliance.

Precise detection of these dormant accounts often proves delicate. IT administrators generally rely on LastLogonDate or lastLogonTimestamp attributes, which however present significant limitations due to replication delays that can reach 14 days. This imprecision makes audits unreliable.

Why Identify Inactive Active Directory Users

Dormant accounts in your Active Directory infrastructure generate several concrete problems:

  • Increased security risks: Inactive accounts remain active by default, offering potential attack vectors
  • Compliance audit complexity: Access reviews become laborious with hundreds of ghost accounts
  • Administrative overload: Management becomes unnecessarily burdensome, consuming precious resources
  • License issues: Service or unnecessary user accounts generate superfluous costs
  • Governance violations: Non-compliance with security policies and compliance frameworks
✦

Governance tip

A quarterly review of inactive accounts is a good practice for maintaining a healthy and secure Active Directory infrastructure.

Limitations of Native Active Directory Tools

Graphical tools provided by Microsoft do not effectively identify inactive users based on their true last logon. The lastLogon attribute provides the most accurate information, but querying it requires accessing each domain controller individually.

To overcome this limitation, a PowerShell approach emerges as the optimal solution. It allows querying all domain controllers, comparing returned values and accurately determining the actual last logon of each user.

PowerShell Script to Detect Inactive Users

Main Script Features

A PowerShell script dedicated to identifying dormant users offers the following functionality:

  • Retrieval of all inactive users by querying each domain controller
  • Comparison of lastLogon attributes to determine actual last logon
  • Detailed export to formatted CSV report
  • Identification based on configurable inactivity period
  • Generation of separate reports for enabled and disabled accounts
  • Detection of accounts never used since creation
  • Targeted retrieval of dormant accounts in specific organizational units (OUs)
  • Automatic installation of Active Directory PowerShell module if absent
  • Compatibility with Windows Task Scheduler

Report of Inactive Active Directory Users

The generated report contains critical information: username, account status, last logon, number of inactive days, OU path, department, job title and account creation date.

Methods for Running the PowerShell Script

Interactive Script Execution

1

Download the PowerShell script

Obtain the GetADInactiveUsers.ps1 script and save it on your administration workstation.

2

Open PowerShell as Administrator

Launch Windows PowerShell with administrator privileges on a machine with the Active Directory module.

3

Navigate to the Script Directory

Change the current directory to the location where the script was saved.

4

Execute the Interactive Script

To identify all users inactive for 90 days, execute the following command:

⚡PowerShell
1.\GetADInactiveUsers.ps1 -InactiveDays 90

The script will guide you through the execution steps and generate a detailed report.

!

System Requirements

This script requires Windows Server or Windows Professional/Enterprise edition supporting RSAT and the Active Directory PowerShell module. Home editions are not compatible.

Automatic Script Scheduling

For continuous monitoring without manual intervention, integrate the script into Windows Task Scheduler:

1

Open Task Scheduler

Launch Task Scheduler on your administration server.

2

Create a New Task

Create a scheduled task with the required execution (for example, weekly or monthly).

3

Configure the Execution Action

In the Actions section, set the program to run and use the -Unattended parameter to suppress interactive prompts:

⚡PowerShell
1.\GetADInactiveUsers.ps1 -Unattended

The -Unattended parameter makes the script compatible with scheduled execution without user intervention.

4

Configure Account Permissions

Ensure that the service account used by Task Scheduler has the following permissions:

  • Read Active Directory data on all domain controllers
  • Right to log on as a scheduled task
5

Verify Report Generation

The CSV report will be automatically generated and saved in the script's current execution directory.

Generating Targeted Inactive User Reports

The script includes native filters allowing you to generate specific reports according to your operational needs.

Identify Active but Inactive Users

Enabled accounts dormant for a long time constitute a direct vulnerability. They retain full access to organizational resources while no longer being used.

⚡PowerShell
1.\GetADInactiveUsers.ps1 -InactiveDays 90 -EnabledUsersOnly

This command exports all enabled accounts inactive for 90 days. Administrators can then disable these accounts or review their access permissions.

To refine further, exclude accounts that have never logged in:

⚡PowerShell
1.\GetADInactiveUsers.ps1 -InactiveDays 90 -EnabledUsersOnly -ExcludeNeverLoggedInUsers

This approach focuses on previously active accounts now dormant, excluding provisioning errors.

Identify Disabled Users Eligible for Deletion

Many organizations follow a gradual cleanup policy: inactive accounts are first disabled, then deleted after an additional period.

⚡PowerShell
1.\GetADInactiveUsers.ps1 -InactiveDays 180 -DisabledUsersOnly

This command retrieves all disabled accounts with no recorded access for 180 days, ideal candidates for permanent deletion.

Spot Never-Used Accounts

Accounts that have never generated authentication often reveal provisioning errors or abandoned projects:

⚡PowerShell
1.\GetADInactiveUsers.ps1 -NeverLoggedInUsersOnly

The report will list all accounts whose lastLogon attribute has never been set.

i

Good to know

This report includes newly created accounts that have not yet had the opportunity to connect. Always verify the creation date before deleting an account.

Analyze Inactive Users by Organizational Unit

In large organizations, users are typically grouped by OU (department, location, function). OU-based analysis enables targeted cleanup:

⚡PowerShell
1.\GetADInactiveUsers.ps1 -InactiveDays 90 -OU "OU=Sales,OU=Users,DC=contoso,DC=com"

Replace the OU's Distinguished Name (DN) with the one corresponding to your infrastructure. The script automatically includes nested child OUs.

Combine Multiple Filtering Criteria

For more granular reporting, combine available parameters:

⚡PowerShell
1.\GetADInactiveUsers.ps1 -InactiveDays 90 -OU "OU=Sales,OU=Users,DC=contoso,DC=com" -EnabledUsersOnly -ExcludeNeverLoggedInUsers

This command extracts only enabled accounts, inactive for 90 days, in the Sales OU, excluding never-used accounts.

Combineable parameters include:

  • -InactiveDays: Number of inactive days
  • -OU: Distinguished name of the organizational unit
  • -EnabledUsersOnly: Filter on enabled accounts only
  • -DisabledUsersOnly: Filter on disabled accounts only
  • -NeverLoggedInUsersOnly: Display only never-used accounts
  • -ExcludeNeverLoggedInUsers: Exclude accounts that have never logged in

Comparison Table of Detection Methods

ApproachAccuracyActive Directory ReplicationComplexity
LastLogonDate (GUI)MediumAffected by delays (14 days)Very low
lastLogonTimestampMediumAffected by delays (14 days)Low
lastLogon with PowerShellVery highNot affectedMedium
Optimized automated scriptVery highNot affectedLow

Best Practices for Managing Inactive Accounts

Ă—

Important

Never permanently delete an inactive account without complete audit. Some service or application accounts must remain active even if they show little access.

To optimize the management of inactive users:

  • Establish an inactivity policy: Define a clear threshold (for example 90 days) triggering actions
  • Implement a progressive process: Disable first, delete after an additional period
  • Perform regular reviews: Schedule monthly or quarterly audits
  • Document deletions: Keep a history of deleted accounts for compliance
  • Exclude critical accounts: Configure the script to ignore service or administrator accounts
  • Notify managers: Warn managers before disabling accounts of their teams
  • Archive data: Retain reports for regulatory traceability

Integration with Active Directory Security

Detecting inactive users integrates into a comprehensive security strategy. In addition:

  • See our guides on securing Active Directory
  • Examine access audits supplemented with inactivity data
  • Synchronize your results with identity management tools (Entra ID for hybrid environments)
  • Combine with permission audits to identify overactive or misconfigured accounts

Conclusion

Proactive management of inactive users in Active Directory is an essential component of organizational security posture. Using an automated PowerShell script eliminates imprecisions of native tools and enables reliable identification based on the true last logon of each user.

By implementing the approaches detailed in this guide—interactive or scheduled execution, granular filtering, and progressive cleanup process—you will significantly reduce your attack surface and improve regulatory compliance.

Do not wait for your dormant accounts to become a liability: implement a monthly review starting today.

Share:
HM

Houssem MAKHLOUF

Microsoft 365 enthusiast & IT professional.

Previous article

Microsoft 365 Copilot Updates: June 2026

Jun 25, 2026
Next article

Detect Inactive Teams Channels with PowerShell

Jun 25, 2026

Related articles

Exécution de scripts PowerShell pour auditer des applications AI et gérer leurs enregistrements.copilot

Audit and Manage AI Applications with PowerShell

Audit unauthorized AI applications in Entra ID with PowerShell and Microsoft Graph to strengthen control and security.

Jun 28, 20264 min
Graphiques abstraits et géométriques avec des couches de couleurs translucides.exchange

Converting Exchange IDs for Microsoft 365 Graph API

Convert Exchange identifiers (storeId, entryId, RestId) for Graph API and targeted eDiscovery. Technical guide with complete PowerShell scripts.

Jun 28, 20267 min
Pyramide réfléchissante au centre de réseaux de fils dorés et cercles.azure

Graph Delta Queries for Entra ID Groups

Learn how to use Graph Delta Queries for Entra ID groups to track changes in real-time. Tutorials and scripts included.

Jun 27, 20264 min