Export unique AD departments

This topic contains 2 replies, has 2 voices, and was last updated by  mega128 1 year, 10 months ago.

  • Author
    Posts
  • #32080

    mega128
    Participant

    Hello,

    Below script works fine. How can I filter only the unique departments in the domain? Departments are repeated because many users can be member of the same department. I am looking for a list of all non-repeated departments in the domain.

    Get-ADUser -Filter * -SearchBase "OU=Users,...,DC=COM" -Properties * | Select-Object Company, department | Export-Csv "C:\powerShell\export.csv"
    

    Thanks

  • #32081

    Richard Siddaway
    Moderator

    If you just want departments

    Get-ADUser -Filter * -SearchBase "OU=Users,...,DC=COM" -Properties Department |
    Select-Object -expandproperty department | select -Unique

    or

    Get-ADUser -Filter * -SearchBase "OU=Users,...,DC=COM" -Properties Department |
    Select-Object -expandproperty department | sort -Unique

    Note that select is case sensitive but sort is case insensitive when using -Unique switch

    You could also use the Get-Unique cmdlet

  • #32085

    mega128
    Participant

    This is perfect! Another quick question:

    I want to sort by unique department then sort by Company name.
    How do I get around that?

    Thanks again 🙂

You must be logged in to reply to this topic.