Solved

Powershell - Select-Object -Unique vs Group-Object

Posted on 2013-12-14
3
646 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
A procedure for exporting installed hotfix details of remote computers using powershell
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now