Solved

Powershell - Select-Object -Unique vs Group-Object

Posted on 2013-12-14
3
724 Views
Last Modified: 2014-01-06
Hi,

I have the following problem with powershell.

I have a list of strings call it $machine_list which
I know there are duplicates.

The following  code produces the following output:

$machine_list.count -- 15375

$a = $machine_list | Select-Object -Unique
$a.Count -- 12134

$b = $machine_list | Group-Object -NoElement
$b.Count -- 12082

Open in new window


I am trying to get a unique list and looking at different
ways of doing it.

So in my example above why are the counts different?
Should they not be the same - $a.Count -eq $b.Count?

I am hoping somebody can explain this in more detail to me.

Also is there a way I can compare the results to see how they
differ? (Comparing $a with the Name Values of $b).

Thanks,

Ward.
0
Comment
Question by:whorsfall
  • 2
3 Comments
 
LVL 44

Expert Comment

by:Rainer Jeschor
ID: 39718737
Hi Ward,
could you provide the machine list? How did you create this list of strings?

As far as I can remember, the unique command needs to have the list sorted.
What does this return:
$a = $machine_list | Sort-Object | Select-Object -Unique

Open in new window


HTH
Rainer
0
 
LVL 44

Accepted Solution

by:
Rainer Jeschor earned 250 total points
ID: 39718747
Hi,

OK - I did a quick test:
Select-Object is case-sensitive
Sort-Object is NOT case-sensitive

Sample:
$machineList = @("abc","aBc","DEF","ABC","def","DEF")

Open in new window


1. Sort-Object
$result = $machineList | Sort-Object -Unique

Open in new window

returns just ABC and DEF

2. Select-Object
$result = $machineList | Select-Object -Unique

Open in new window

returns all unique values - even if the difference is just upper/lowercase

3. Group-Object
$result = $machineList | Group-Object -NoElement

Open in new window

returns abc and DEF.

HTH
Rainer
0
 
LVL 39

Assisted Solution

by:footech
footech earned 250 total points
ID: 39719008
RainerJ provided a very good comparison of the different commands.

A couple things I wanted to expand on though...
- There's also the Get-Unique cmdlet, which is the only one that needs the input to be sorted before it is sent to it.
- Sort-Object and Group-Object also include  a -CaseSensitive switch parameter which can affect the results.

You can try using Compare-Object, but the objects need to have the same properties (or be of the same type).  So to make $b like $a, use
$b = $machine_list | Group-Object -NoElement | Select -ExpandProperty name

Open in new window

Then
compare $a $b

Open in new window

This might give you some insight.  However, I'm hesitant to suggest it in this situation since when there are duplicates within the objects (as opposed to between them), it doesn't do a good job (i.e. doesn't output as I would expect).
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Utilizing an array to gracefully append to a list of EmailAddresses
In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question