PowerShell Ccript for Counting Enabled and Disabled Users in Active Directory

Managing user accounts in Active Directory (AD) is a critical aspect of IT administration. PowerShell, a robust scripting language developed by Microsoft, empowers administrators to streamline routine tasks. In this blog post, we’ll explore an enhanced PowerShell script to not only count the total number of users in Active Directory but also categorize them into enabled and disabled accounts.

Prerequisites: Before running the script, ensure that you have the Active Directory module installed on your machine. Use the following PowerShell command to install it:

Install-Module -Name RSAT-AD-PowerShell -Force

The script below utilizes the Get-ADUser cmdlet to retrieve user information, categorize accounts as enabled or disabled, and provide a comprehensive summary

# Import the Active Directory module
Import-Module ActiveDirectory

# Get all users from Active Directory
$users = Get-ADUser -Filter *

# Count the total number of users
$totalUsers = $users.Count

# Count the number of enabled users
$enabledUsers = ($users | Where-Object { $_.Enabled -eq $true }).Count

# Count the number of disabled users
$disabledUsers = ($users | Where-Object { $_.Enabled -eq $false }).Count

# Display the results
Write-Host "Total number of users in Active Directory: $totalUsers"
Write-Host "Number of enabled users: $enabledUsers"
Write-Host "Number of disabled users: $disabledUsers"


Explanation for above Powershell script

  1. Total Users Count: The variable $totalUsers stores the total number of user accounts in Active Directory.
  2. Enabled Users Count: The variable $enabledUsers is calculated by filtering the user collection to include only enabled users.
  3. Disabled Users Count: The variable $disabledUsers is calculated by filtering the user collection to include only disabled users.
  4. Displaying the Results: The script showcases a detailed breakdown, displaying the total number of users, the number of enabled users, and the number of disabled users.

Conclusion: This enhanced PowerShell script provides administrators with a quick and efficient way to gain insights into user accounts within Active Directory. By categorizing users into enabled and disabled accounts, administrators can better understand their user base, enabling them to make informed decisions and maintain a secure and well-managed Active Directory environment. PowerShell’s flexibility and integration with Active Directory continue to make it an indispensable tool for IT professionals tasked with user management in Windows environments.

You can export all Active directory users using PowerShell script. Below is the link for the same.