PowerShell Genie

Find users without a department

Script 2 min read Windows PowerShell 5.1 and PowerShell 7

Documentation: Microsoft Learn

Enabled accounts come back with the Department attribute requested, the middle stage keeps the ones where it is empty, and the output is narrowed to the account name.

The script

1PS> Get-ADUser -Filter "Enabled -eq $true" -Properties Department | Where-Object { -not $_.Department } | Select-Object SamAccountName,Name

Script reader

Inspect before you run

Inspection only

Inspect the script below. It does not run in this browser.

Inspection only — no command has run.

    Show reference output

    Illustrative rows: Department is empty for the returned accounts.

    SamAccountName Name
    -------------- ----
    msen           Mira Sen
    adas           Asha Das
    Inspect this example’s structure

    Read the command and its supplied arguments. This example requires real PowerShell or its module; this diagram does not execute it.

    Get-ADUser -Filter "Enabled -eq $true" -Properties Department | Where-Object { -not $_.Department } | Select-Object SamAccountName,Name
    1. Stage 1Get-ADUser-Filter -Properties
    2. Stage 2Where-ObjectPositional values or no named parameters
    3. Stage 3Select-ObjectPositional values or no named parameters

      How it works

      Enabled accounts come back with the Department attribute requested, the middle stage keeps the ones where it is empty, and the output is narrowed to the account name.

      Adapting it

      Substitute Title or EmployeeID for Department depending on which attribute your dynamic rules actually key off; the shape of the query does not change at all.

      Requirements

      ActiveDirectory module.

      If the result is unexpected

      The department report returns many accounts that appear correctly assigned elsewhere.

      Department may be stored in HR or cloud data without being populated in the queried AD attribute.

      1. Inspect the requested Department property and compare a sample with the authoritative source.

      Validate the result

      The report identifies genuinely empty AD department values using the stated scope.

      Keep the check reliable

      Define which system supplies Department and reconcile changes through that system.

      Primary documentation

      PowerShell objects and pipelines documentation

      Continue
      Genie guideFind users without a department

      Enabled accounts come back with the Department attribute requested, the middle stage keeps the ones where it is empty, and the output is narrowed to the account name.