Solved

Powershell - Select-Object -Unique vs Group-Object

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

770 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