Solved

Powershell - Select-Object -Unique vs Group-Object

Posted on 2013-12-14
3
670 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 Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
Set OWA language and time zone in Exchange for individuals, all users or per database.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

895 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

14 Experts available now in Live!

Get 1:1 Help Now