Removing duplicate values ​​from a PowerShell array

How to remove duplicates from a PowerShell array?

$a = @(1,2,3,4,5,5,6,7,8,9,0,0) 
+99
arrays powershell
Sep 08 '09 at 3:41
source share
7 answers

Use Select-Object (whose alias is select ) with the -Unique switch; eg.:

 $a = @(1,2,3,4,5,5,6,7,8,9,0,0) $a = $a | select -Unique 
+173
Sep 08 '09 at 3:46
source share

Another option is to use Sort-Object (whose alias is sort , but only on Windows) with
-Unique , which combines sorting with duplicate removal:

 $a | sort -unique 
+81
Sep 08 '09 at 6:54
source share
 $a | sort -unique 

This works case-sensitive, so duplicate rows with different registers are deleted. Solved my problem.

 $ServerList = @( "FS3", "HQ2", "hq2" ) | sort -Unique $ServerList 

The above findings:

 FS3 HQ2 
+6
Apr 24 '17 at 18:50
source share

If the list is sorted, you can use the Get-Unique cmdlet:

  $a | Get-Unique 
+4
May 26 '16 at 18:32
source share

In case you want to be a fully proven bomb, this is what I would advise:

 @('Apples', 'Apples ', 'APPLES', 'Banana') | Sort-Object -Property @{Expression={$_.Trim()}} -Unique 

Output:

 Apples Banana 

This uses the Property parameter for the first line of Trim() , so extra whitespace is removed, and then only -Unique values ​​are selected.

Additional information about Sort-Object :

 Get-Help Sort-Object -ShowWindow 
+4
Jun 01. '17 at 10:58 on
source share

Whether you use SORT -UNIQUE , SELECT -UNIQUE or GET-UNIQUE from Powershell 2.0 to 5.1, all of the above examples are for single-column arrays. I have yet to get this to work in arrays with multiple columns, to DELETE duplicate rows, to leave single occurrences of the row in the specified columns, or to develop an alternative script solution. Instead, these cmdlets only returned rows in an array that happened ONCE in the singular and flushed everything that had a duplicate. Usually I have to delete duplicates manually from the final CSV output in Excel to complete the report, but sometimes I would like to continue working with the specified data in Powershell after deleting the duplicates.

+2
Nov 08 '17 at 1:30
source share

Using my method, you can completely remove duplicate values, leaving you with the values ​​from an array that only had the number 1. It’s not clear if this is really what the OP really wanted, but I couldn’t find an example of this solution on the Internet, so here .

 $array=@' Bananna Apple Carrot Pear Apricot Pear Bananna '@ -split '\r\n' ($array | Group-Object -NoElement | ?{$_.count -eq 1}).Name 
+1
Jul 24 '18 at 21:56
source share



All Articles