从PowerShell数组中删除重复的值

时间:2023-02-09 13:32:41

How can I remove duplicates from a PowerShell array?

如何从PowerShell数组中删除副本?

$a = @(1,2,3,4,5,5,6,7,8,9,0,0)

6 个解决方案

#1


141  

Use Select -uniq e.g.:

使用选择uniq如:

$a = @(1,2,3,4,5,5,6,7,8,9,0,0)
$a = $a | select -uniq

#2


63  

Another option:

另一个选择:

$a | sort -unique

#3


3  

$a | sort -unique

This works with case-insensitive, therefore removing duplicates strings with differing cases. Solved my problem.

这是不区分大小写的,因此删除具有不同大小写的重复字符串。解决了我的问题。

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

$ServerList

FS3

HQ2

#4


2  

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

如果列表被排序,您可以使用Get-Unique cmdlet:

 $a | Get-Unique

#5


2  

In case you want to be fully bomb prove, this is what I would advice:

如果你想要完全证明,这是我的建议:

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

Output:

输出:

Apples
Banana

This uses the Property parameter to first Trim() the strings, so extra spaces are removed and then selects only the -Unique values.

这使用属性参数首先修饰字符串,因此删除额外的空格,然后只选择-Unique值。

More info on Sort-Object:

更多信息在sort对象:

Get-Help Sort-Object -ShowWindow

#6


0  

Whether you're using "SORT -UNIQUE", "SELECT -UNIQUE" or "GET-UNIQUE" from Powershell 2.0 to 5.1, all the examples given are on single Column arrays. I have yet to get this to function across Arrays with multiple Columns to REMOVE Duplicate Rows to leave single occurrences of a Row across said Columns, or develop an alternative script solution. Instead these cmdlets have only returned Rows in an Array that occurred ONCE with singular occurrence and dumped everything that had a duplicate. Typically I have to Remove Duplicates manually from the final CSV output in Excel to finish the report, but sometimes I would like to continue working with said data within Powershell after removing the duplicates.

无论您是在Powershell 2.0到5.1中使用“SORT -UNIQUE”、“SELECT -UNIQUE”还是“GET-UNIQUE”,所给出的所有示例都位于单个列数组中。我还没有让它在具有多个列的数组中起作用,以便删除重复的行,以便在这些列中保留单个行,或者开发另一种脚本解决方案。相反,这些cmdlet只返回发生了一次的数组中的行,并转储具有重复的所有内容。通常情况下,我必须从Excel的最终CSV输出中手动删除副本,以完成报告,但有时我希望在删除重复项之后继续使用Powershell中的表示数据。

#1


141  

Use Select -uniq e.g.:

使用选择uniq如:

$a = @(1,2,3,4,5,5,6,7,8,9,0,0)
$a = $a | select -uniq

#2


63  

Another option:

另一个选择:

$a | sort -unique

#3


3  

$a | sort -unique

This works with case-insensitive, therefore removing duplicates strings with differing cases. Solved my problem.

这是不区分大小写的,因此删除具有不同大小写的重复字符串。解决了我的问题。

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

$ServerList

FS3

HQ2

#4


2  

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

如果列表被排序,您可以使用Get-Unique cmdlet:

 $a | Get-Unique

#5


2  

In case you want to be fully bomb prove, this is what I would advice:

如果你想要完全证明,这是我的建议:

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

Output:

输出:

Apples
Banana

This uses the Property parameter to first Trim() the strings, so extra spaces are removed and then selects only the -Unique values.

这使用属性参数首先修饰字符串,因此删除额外的空格,然后只选择-Unique值。

More info on Sort-Object:

更多信息在sort对象:

Get-Help Sort-Object -ShowWindow

#6


0  

Whether you're using "SORT -UNIQUE", "SELECT -UNIQUE" or "GET-UNIQUE" from Powershell 2.0 to 5.1, all the examples given are on single Column arrays. I have yet to get this to function across Arrays with multiple Columns to REMOVE Duplicate Rows to leave single occurrences of a Row across said Columns, or develop an alternative script solution. Instead these cmdlets have only returned Rows in an Array that occurred ONCE with singular occurrence and dumped everything that had a duplicate. Typically I have to Remove Duplicates manually from the final CSV output in Excel to finish the report, but sometimes I would like to continue working with said data within Powershell after removing the duplicates.

无论您是在Powershell 2.0到5.1中使用“SORT -UNIQUE”、“SELECT -UNIQUE”还是“GET-UNIQUE”,所给出的所有示例都位于单个列数组中。我还没有让它在具有多个列的数组中起作用,以便删除重复的行,以便在这些列中保留单个行,或者开发另一种脚本解决方案。相反,这些cmdlet只返回发生了一次的数组中的行,并转储具有重复的所有内容。通常情况下,我必须从Excel的最终CSV输出中手动删除副本,以完成报告,但有时我希望在删除重复项之后继续使用Powershell中的表示数据。