PowerShell Script to Get Disabled AD Users Easily
Get-ADUser -Filter 'Enabled -eq $false' -Properties Enabled | Select-Object Name, Enabled to list all disabled Active Directory users.Examples
How to Think About It
Algorithm
Code
Import-Module ActiveDirectory
$disabledUsers = Get-ADUser -Filter 'Enabled -eq $false' -Properties Enabled | Select-Object Name, Enabled
Write-Output $disabledUsersDry Run
Let's trace a query where AD has two disabled users: John Doe and Jane Smith.
Run Get-ADUser with filter
Get-ADUser -Filter 'Enabled -eq $false' returns John Doe and Jane Smith objects.
Select Name and Enabled properties
Select-Object extracts Name and Enabled from each user.
Output the list
Write-Output prints the list showing names and Enabled = False.
| Name | Enabled |
|---|---|
| John Doe | false |
| Jane Smith | false |
Why This Works
Step 1: Filtering Disabled Users
The -Filter 'Enabled -eq $false' tells PowerShell to get only users whose Enabled property is false, meaning disabled.
Step 2: Selecting Relevant Properties
Using Select-Object Name, Enabled shows only the user name and their enabled status for clarity.
Step 3: Outputting the Result
The Write-Output command prints the list to the console so you can see which users are disabled.
Alternative Approaches
Get-ADUser -LDAPFilter "(userAccountControl:1.2.840.113556.1.4.803:=2)" | Select-Object NameGet-ADUser -Filter * -Properties Enabled | Where-Object { $_.Enabled -eq $false } | Select-Object Name, EnabledComplexity: O(n) time, O(n) space
Time Complexity
The command queries all users matching the filter, so time grows linearly with the number of users in AD.
Space Complexity
The script stores the filtered user list in memory, so space grows with the number of disabled users.
Which Approach is Fastest?
Filtering directly in Get-ADUser is fastest; filtering later with Where-Object is slower.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Filter in Get-ADUser | O(n) | O(n) | Efficient for large AD environments |
| Where-Object filtering | O(n) | O(n) | Simple but slower for big data |
| LDAP Filter | O(n) | O(n) | Advanced LDAP users, less readable |
Import-Module ActiveDirectory.