Why AD management is essential for sysadmins in PowerShell - Performance Analysis
Managing Active Directory (AD) with PowerShell helps sysadmins automate tasks efficiently.
We want to understand how the time to manage AD grows as the number of users or computers increases.
Analyze the time complexity of the following PowerShell script that lists all AD users and disables those inactive for 90 days.
$users = Get-ADUser -Filter * -Properties LastLogonDate
foreach ($user in $users) {
if ($user.LastLogonDate -lt (Get-Date).AddDays(-90)) {
Disable-ADAccount -Identity $user.SamAccountName
}
}
This script gets all users, checks their last login date, and disables inactive accounts.
- Primary operation: Looping through each user in the AD user list.
- How many times: Once for every user returned by Get-ADUser.
As the number of users grows, the script checks each user one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks and possible disables |
| 100 | About 100 checks and possible disables |
| 1000 | About 1000 checks and possible disables |
Pattern observation: The work grows directly with the number of users.
Time Complexity: O(n)
This means the time to complete the task grows in a straight line as the number of users increases.
[X] Wrong: "The script runs instantly no matter how many users there are."
[OK] Correct: Each user must be checked, so more users mean more work and more time.
Understanding how scripts scale with input size shows you can write efficient automation for real systems.
"What if we filtered users directly in Get-ADUser to only inactive accounts? How would the time complexity change?"