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.

Export all users Powershell script

By amit_g

Welcome to my IT Infra Blog! My name is Amit Kumar, and I am an IT infrastructure expert with over 11 years of experience in the field. Throughout my career, I have worked with a wide variety of systems and technologies, from network infrastructure and cloud computing to hardware and software development. On this blog, I aim to share my knowledge, insights, and opinions on all things related to IT infrastructure. From industry trends and best practices to tips and tricks for managing complex systems, my goal is to provide valuable information that will help IT professionals and enthusiasts alike. Whether you are a seasoned IT veteran or just getting started in the field, I hope you will find my blog to be a valuable resource. In addition to sharing my own thoughts and ideas, I also welcome feedback, comments, and questions from my readers. I believe that a collaborative approach is the best way to advance the field of IT infrastructure and I look forward to hearing from you. Thank you for visiting my blog, and I hope you will continue to follow along as I explore the fascinating world of IT infrastructure. Sincerely, Amit Kumar

Leave a Reply

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