Link to home
Start Free TrialLog in
Avatar of Kasper Katzmann
Kasper KatzmannFlag for Denmark

asked on

Get unique AD values with powershell

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
Avatar of Rajitha Chimmani
Rajitha Chimmani
Flag of United States of America image

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
Avatar of Kasper Katzmann

ASKER

None of the above works.

The first gives nothing
The second gives all user with all properties
The third gives a single user
get-aduser -Resultsetsize $null | Group-Object -Property Title | Select Name
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
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).
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
ASKER CERTIFIED SOLUTION
Avatar of SubSun
SubSun
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yep, thats the one, nice. Thanks.

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

Thanks again.