Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Powershell: Issues with Get-unique

Posted on 2010-11-15
4
Medium Priority
?
938 Views
Last Modified: 2012-05-10
I have two questions regarding the get-unique cmdlet.

One:
If I have a txt file with 100 computer names with duplicates I want the duplicates to be removed.
I have the following lines but they do not remove duplicates properly.

[array]$colComputers = get-content "c:\temp\ISP\listAllComputers.txt" | Sort-Object | Get-Unique

no duplicates are removed. Am I using this right?

Question two:
Is there a way to invoke the get-unique where it does not care or check for uppercase or lowercase?
For example with the following txt file entries:
apple
pear
APPLE
ApPle

there will be only two ouputs
apple
pear

thanks in advance,
K
0
Comment
Question by:chad
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
daveTechSearch earned 1000 total points
ID: 34138657
try this instead:
[array]$colComputers = get-content "c:\temp\ISP\listAllComputers.txt" | Sort-Object -unique
0
 
LVL 27

Expert Comment

by:KenMcF
ID: 34138691
I do not think you can account for different case. You could create an array and change all computer account to upper or lower case so they all match then select the unique ones.


$cmps = @()
gc "c:\temp\ISP\listAllComputers.txt" | %{$cmps += $_.toupper()}
$cmps | sort | get-unique
0
 
LVL 27

Expert Comment

by:KenMcF
ID: 34138711
The way daveTechSearch mentioned, is much easier. I forgot that the switch in select was not case sensitive.
0
 
LVL 11

Author Comment

by:chad
ID: 34139308
Thanks Dave,

Using that technique did solve both concerns.
I had a feeling I may end up having to go the route you KenMcF mentioned.

Thank you both,
K

0

Featured Post

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

Question has a verified solution.

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

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
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ā€¦
Loops Section Overview

885 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