Kasper Katzmann
asked on
Get unique AD values with powershell
I have this script:
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
get-aduser -filter * -properties title | get-unique
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
ASKER
None of the above works.
The first gives nothing
The second gives all user with all properties
The third gives a single user
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
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).
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).
ASKER
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.Pow erShell.Co mmands.Sel ec
tObjectCommand
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.Pow
tObjectCommand
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yep, thats the one, nice. Thanks.
I added "| Sort-Object Title" in the end to sort it alphabetically, just because I could :-)
Thanks again.
I added "| Sort-Object Title" in the end to sort it alphabetically, just because I could :-)
Thanks again.
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