Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Powershell - Select-Object -Unique vs Group-Object

Posted on 2013-12-14
3
Medium Priority
?
873 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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 41

Assisted Solution

by:footech
footech earned 1000 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

722 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