Find domain disabled users (in all sub-OU's)

Andrea Rochira 1 Reputation point
2020-12-05T00:05:59.033+00:00

Dear all,

I'm working on a script that can find disabled user accounts within any Active Directory sub-OU of the domain. I tried the cmdlets Search-ADAccount and Get-ADUser, but it always ends up finding only two disabled user accounts located in the built-in Users OU. No greater luck by using the -SearchBase and -SearchScope parameters to target a specific OU or explicitly perform the search in all the sub-OU's. See image below:

45385-screen-shot-2020-12-04-at-50003-pm.png

I have read other similar threads, but none of the solutions proposed worked for me.

I can't figure out how to recursively look within sub-OU's and return all the disabled users scattered within them. If I use the console Active Directory Users and Computers to create a new saved query, it works just fine, but I need to automate the task through a PowerShell script: moving all the disabled user accounts to a specific OU before deletion (in any case, I need to first be able to find them).

Any input will be much appreciated. Thanks in advance!

Andrea

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,364 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Andrea Rochira 1 Reputation point
    2020-12-05T19:14:26.91+00:00

    Thank you @Rich Matheisen ,

    It was as easy as running PowerShell ISE as Admin to solve the riddle. Since I was logged in with a domain admin account and since the strings were returning something (those 2 disabled accounts in the built-in Users "container"), I didn't think I needed to run PS ISE with higher privileges.

    All 4 command strings you see in my screenshot now work (with or without the specification of a target OU).

    Regards.

    0 comments No comments

  2. Kailas 1 Reputation point
    2021-09-22T04:43:08.297+00:00

    Get-ADUser -Filter * -Property Enabled | Where-Object {$_.Enabled -like "false"} | ft Name, Enabled -Autosize

    0 comments No comments

  3. Alper GÜNER 0 Reputation points
    2023-09-18T09:28:34.47+00:00

    Hello dear everyone,

    How can I filter accounts whose user accounts have been closed within this year?

    I will search all accounts, if the account is closed within the date range I specified, the information should be written on the screen. Can you help with this issue.