PowerShell Script to Identify Locked Users

Managing user accounts and security is a crucial aspect of maintaining a healthy Active Directory environment. One common scenario administrators encounter is identifying and managing locked user accounts. In this blog post, we’ll explore a PowerShell script that makes it easy to pinpoint locked users in Active Directory.

Prerequisites:

Before diving into the script, ensure that you have the necessary permissions and the Active Directory module installed on your machine. You can install the module by adding the “Remote Server Administration Tools (RSAT)” feature.

The PowerShell Script:

# Import the Active Directory module
Import-Module ActiveDirectory

# Get locked users in Active Directory
$lockedUsers = Get-ADUser -Filter {LockedOut -eq $true} -Properties SamAccountName,LockedOut

# Display the locked users
foreach ($user in $lockedUsers) {
    Write-Host "User: $($user.SamAccountName) is locked out."
}

How to Use:

  1. Save the Script: Save the script with a .ps1 extension (e.g., Get-LockedUsers.ps1).
  2. Run the Script: Open a PowerShell window, navigate to the directory where the script is saved, and execute the script using the following command:

Understanding the Script:

  • Import-Module ActiveDirectory: This line imports the Active Directory module, allowing the use of cmdlets related to Active Directory.
  • Get-ADUser -Filter {LockedOut -eq $true} -Properties SamAccountName,LockedOut: This cmdlet retrieves user accounts that are currently locked out. It filters users based on the ‘LockedOut’ property being equal to $true and fetches additional properties like SamAccountName for display.
  • foreach ($user in $lockedUsers): Iterates through the collection of locked users retrieved from the previous step.
  • Write-Host “User: $($user.SamAccountName) is locked out.”: Displays the SamAccountName of each locked user.

Conclusion:

This PowerShell script simplifies the process of identifying locked users in your Active Directory environment. Regularly running this script can help administrators stay on top of security, promptly addressing locked accounts and ensuring a smooth user experience.

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 *