Export unique AD departments

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

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

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 :slight_smile: