PowerShell Genie

Build an Active Directory account review

The IT team needs a list of disabled accounts in its department. The exercise is a review, not an instruction to enable or delete anybody. Query the right properties and make the scope visible in the report.

Interactive guide 10 min practice Windows PowerShell 5.1 and PowerShell 7

Editable pipeline · fictional data · Microsoft documentation

The task

Get-ADUser returns user objects. Enabled is in the default property set; Department must be requested with -Properties in a real query. Select-Object can choose a property that is already present, but cannot retrieve an attribute the directory never returned.

  • Treat Enabled as a Boolean, not the string "False".
  • Separate directory-side query scope from client-side shaping.
  • Return account identifiers alongside department evidence.

Genie tip: Treat Enabled as a Boolean, not the string "False".

See the objects move

Inspect this authored example with fictional data in the bounded browser model. The numbered stages show how its objects change.

Get-ADUser -Filter * -Properties Department | Where-Object Enabled -eq $false | Where-Object Department -eq 'IT' | Sort-Object SamAccountName | Select-Object SamAccountName,Department
  1. Stage 1Get-ADUser-Filter -Properties
  2. Stage 2Where-Object-eq
  3. Stage 3Where-Object-eq
  4. Stage 4Sort-ObjectPositional values or no named parameters
  5. Stage 5Select-ObjectPositional values or no named parameters

    Build the reasoning

    Request the report inputs

    1PS> Get-ADUser -Filter * -Properties Department

    This lab supplies five fictional users. In a real domain, add the intended SearchBase and server rather than starting with a domain-wide report.

    Filter the account state

    1PS> Where-Object Enabled -eq $false

    Three accounts are disabled. Their departments differ; disabled does not mean they all belong in the IT review.

    Make the evidence readable

    1PS> Sort-Object SamAccountName | Select-Object SamAccountName,Department

    Account names give the reviewer stable identifiers. Keeping Department makes an incorrect scope easier to spot.

    Try it: predict, run and explain

    Fictional input objects · 5 rows
    NameSamAccountNameEnabledDepartment
    Asha Dasadas$trueIT
    Ravi Senrsen$falseFinance
    Nina Roynroy$trueIT
    Former Contractorcontract01$falseOperations
    Meera Shahmshah$falseIT

    Select your predicted objects before revealing the output. Then edit one stage and inspect the change.

    Predict which objects survive the worked pipeline

    Ctrl / Command + Enter runs the editor. This bounded model uses the displayed fixture; it does not run PowerShell on a computer.

    Predict first, then run the pipeline.

      Find the mistake

      Using a string as account state

      1PS> Where-Object Enabled -eq 'False'

      Use $false. String conversion and PowerShell truthiness are easy to misread when a Boolean property is involved.

      Replacing the review with a change

      1PS> Disable-ADAccount -Identity mshah

      A report and an account change have different acceptance criteria. This exercise performs no writes and does not establish whether any account should be changed.

      Department is empty for every result in a real query, but it is populated in the directory. What should you check first?

      Prove it: write it alone

      Return only disabled IT users, sorted by SamAccountName. Return exactly SamAccountName and Department. The command must still work when another disabled IT user appears.

      The checker also runs your command against a fresh fixture. Matching today’s names alone is not enough.

      Show a worked solution
      1PS> Get-ADUser -Filter * -Properties Department | Where-Object Enabled -eq $false | Where-Object Department -eq 'IT' | Sort-Object SamAccountName | Select-Object SamAccountName,Department

      Read the stages, close this explanation, then write and test your own attempt.

      Take it to a real environment

      For a real OU review, use a scoped Get-ADUser query and move suitable predicates into its -Filter parameter to reduce transferred data. Verify the filter syntax separately from Where-Object syntax. The browser models the displayed read-only subset; it does not test credentials, OU paths or domain permissions.

      Real environment example · not executed here

      1PS> Get-ADUser -Filter 'Enabled -eq $false' -SearchBase 'OU=IT,DC=example,DC=com' -Properties Department | Select-Object SamAccountName,Department

      Verify the details

      Final challenge not completedChoose another guide

      Practise this

      Read domain users

      Import ActiveDirectory first. Return all fictional AD user records.

      Review the lesson

      Genie guideBuild an Active Directory account review

      The IT team needs a list of disabled accounts in its department. The exercise is a review, not an instruction to enable or delete anybody. Query the right properties and make the scope visible in the report.