13 min read
Getting started with PowerShell functions
PowerShell functions are essential tools that enable you to encapsulate code for reuse, making your scripts more...
ScriptRunner Blog
Automating recurring Active Directory tasks with PowerShell is key for successful IT professionals and system engineers.
In the world of IT administration, efficiency and accuracy are paramount. Microsoft's PowerShell is a powerful tool that enables administrators to automate recurring tasks in Active Directory (AD), reducing the time and effort required for routine operations and minimizing the risk of human error. In this article, we'll explore the benefits of using PowerShell for AD tasks and delve into five real-life use cases, complete with PowerShell code snippets.
Let's take a look at some real-life use cases for Active Directory PowerShell automation.
In an organization, it's common to onboard multiple employees at once. PowerShell can automate the creation of user accounts in bulk, which is much more efficient than creating them manually.
$users = Import-Csv -Path "C:\NewUsers.csv"
foreach ($user in $users) {
New-ADUser -Name $user.Name -GivenName $user.FirstName -Surname $user.LastName -SamAccountName $user.Username -UserPrincipalName $user.UPN -Path "OU=Users,DC=example,DC=com" -AccountPassword (ConvertTo-SecureString $user.Password -AsPlainText -Force) -Enabled $true
}
When a security breach occurs, or as a routine security measure, you might need to reset passwords for multiple users. PowerShell can streamline this process.
$users = Get-Content -Path "C:\AffectedUsers.txt"
foreach ($user in $users) {
Set-ADAccountPassword -Identity $user -NewPassword (ConvertTo-SecureString -AsPlainText "NewPassword123!" -Force) -Reset
}
Inactive AD accounts pose potential security risks. PowerShell can help identify and disable or remove accounts that have not been used within a specified period. The following code examples contain the -WhatIf parameter. It allows simulating and test the results first.
$inactiveDays = 90
$inactiveDate = (Get-Date).AddDays(-$inactiveDays)
$inactiveUsers = Get-ADUser -Filter {LastLogonTimestamp -lt $inactiveDate -and Enabled -eq $true}
foreach ($user in $inactiveUsers) {
Disable-ADAccount -Identity $user.SamAccountName -WhatIf
}
Sometimes, you may need to update attributes for multiple users, such as their department or title following a reorganization.
$users = Import-Csv -Path "C:\UserUpdates.csv"
foreach ($user in $users) {
Set-ADUser -Identity $user.Username -Department $user.Department -Title $user.Title
}
Reporting is crucial for audit and compliance. PowerShell can extract detailed information about user accounts, such as their creation dates, last login times, and group memberships.
Get-ADUser -Filter * -Properties WhenCreated, LastLogonDate, MemberOf | Select-Object Name, SamAccountName, WhenCreated, LastLogonDate, @{Name="Groups";Expression={$_.MemberOf -join ","}} | Export-Csv -Path "C:\ADUserReport.csv" -NoTypeInformation
PowerShell is an indispensable tool for managing Active Directory efficiently. By automating routine tasks, PowerShell not only saves time but also enhances security and consistency. The use cases above are just the tip of the iceberg. With PowerShell, the possibilities are vast, and the benefits are significant. Whether you're a seasoned administrator or new to PowerShell, investing time in learning and applying these scripts can significantly improve your AD management processes.
Managing Active Directory is one of the most time-consuming recurring tasks of many IT administrators and system engineers.
Creating and maintaining users and groups, managing OUs and computer accounts, providing detailed AD reports is constantly on the to-do lists.
Let us show you how you can streamline, automate, delegate and monitor all your Active Directory processes and tasks.
Save time, reduce errors, and focus on critical IT projects.
We announce all upcoming webinars in our newsletter, so you don't miss a webinar that's of interest to you.
Apr 1, 2025 by Stéphane van Gulick
PowerShell functions are essential tools that enable you to encapsulate code for reuse, making your scripts more...
Feb 24, 2025 by Heiko Brenn
The latest ScriptRunner release enhances Enterprise IT automation with three powerful features: the new Approval Process
Jan 28, 2025 by Jeffery Hicks
Changelogs keep your software updates clear and organized. Learn the best practices for creating and managing them in...
Heiko Brenn is Head of Marketing and Product Marketing Manager. He has been working in the IT industry for more than 25 years and has extensive expertise in email management, security, collaboration, administration, cloud and automation. He has been working with PowerShell since 2010.