Get unique AD values with powershell

Kasper Katzmann
Kasper Katzmann used Ask the Experts™
on
I have this script:
get-aduser -filter * -properties title | get-unique

Open in new window

It gives me all users details including Title.

How can I get a result, where I only get the unique values for title and nothing else?

Example:
Title
----------------
Bookkeeper
Bookkeeper
CEO
Clerk
Clerk
Manager
Manager

Should be:

Title
----------------
Bookkeeper
CEO
Clerk
Manager
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Rajitha ChimmaniSpecialized Technology Analyst

Commented:
Have you tried group-object?

get-aduser -filter *  | Group-Object -Property Title | Select Name

or

get-aduser -filter * | Sort-Object -Property Title | Get-Unique

or

Get-aduser -filter * | Sort-object title -unique

Instead of -Filter *, you can also use -Resultsetsize $null
Kasper KatzmannSeniorkonsulent

Author

Commented:
None of the above works.

The first gives nothing
The second gives all user with all properties
The third gives a single user
Rajitha ChimmaniSpecialized Technology Analyst

Commented:
get-aduser -Resultsetsize $null | Group-Object -Property Title | Select Name
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

Rajitha ChimmaniSpecialized Technology Analyst

Commented:
On Powershell v2.0 and Windows Server 2003 i got correct results using the below command

Get-User -resultsize unlimited | Group-Object -Property Title | Select Name
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
You can't use get-aduser without -filter. Further, using Group-Object just to make something unique is oversized. This should work and perform much better:
 Get-ADUser -filter * -Resultsetsize $null | select -Expand Title | Get-Unique
It dismissed the superfluous properties at the earliest, and works on only title (as a string, because of -Expand).
Kasper KatzmannSeniorkonsulent

Author

Commented:
This doesn't work either. I get this error:

PS C:\Windows\system32> get-aduser -filter * -resultsetsize $null | select -Expa
nd Title | Get-Unique
Select-Object : Cannot process argument because the value of argument "obj" is
null. Change the value of argument "obj" to a non-null value.
At line:1 char:51
+ get-aduser -filter * -resultsetsize $null | select <<<<  -Expand Title | Get-
Unique
    + CategoryInfo          : InvalidArgument: (:) [Select-Object], PSArgument
   NullException
    + FullyQualifiedErrorId : ArgumentNull,Microsoft.PowerShell.Commands.Selec
   tObjectCommand
IT Infrastructure Architect
Commented:
What about
Get-ADUser  -filter * -properties Title | Select Title -Unique
Kasper KatzmannSeniorkonsulent

Author

Commented:
Yep, thats the one, nice. Thanks.

I added "| Sort-Object Title" in the end to sort it alphabetically, just because I could :-)

Thanks again.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial