从PowerShell阵列中删除重复值

时间:2009-09-08 03:41:59

标签: arrays powershell

如何从PowerShell数组中删除重复项?

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

9 个答案:

答案 0 :(得分:163)

使用Select -uniq例如:

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

答案 1 :(得分:76)

另一种选择:

$a | sort -unique

答案 2 :(得分:6)

$a | sort -unique

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

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

$ServerList

FS3

HQ2

答案 3 :(得分:4)

这就是您如何从具有两个或多个属性的数组中获得唯一性的方法。排序是至关重要的,也是让它正常工作的关键。否则,您只会退回一件商品。

PowerShell 脚本:

$objects = @(
    [PSCustomObject] @{ Message = "1"; MachineName = "1" }
    [PSCustomObject] @{ Message = "2"; MachineName = "1" }
    [PSCustomObject] @{ Message = "3"; MachineName = "1" }
    [PSCustomObject] @{ Message = "4"; MachineName = "1" }
    [PSCustomObject] @{ Message = "5"; MachineName = "1" }
    [PSCustomObject] @{ Message = "1"; MachineName = "2" }
    [PSCustomObject] @{ Message = "2"; MachineName = "2" }
    [PSCustomObject] @{ Message = "3"; MachineName = "2" }
    [PSCustomObject] @{ Message = "4"; MachineName = "2" }
    [PSCustomObject] @{ Message = "5"; MachineName = "2" }
    [PSCustomObject] @{ Message = "1"; MachineName = "1" }
    [PSCustomObject] @{ Message = "2"; MachineName = "1" }
    [PSCustomObject] @{ Message = "3"; MachineName = "1" }
    [PSCustomObject] @{ Message = "4"; MachineName = "1" }
    [PSCustomObject] @{ Message = "5"; MachineName = "1" }
    [PSCustomObject] @{ Message = "1"; MachineName = "2" }
    [PSCustomObject] @{ Message = "2"; MachineName = "2" }
    [PSCustomObject] @{ Message = "3"; MachineName = "2" }
    [PSCustomObject] @{ Message = "4"; MachineName = "2" }
    [PSCustomObject] @{ Message = "5"; MachineName = "2" }
)

Write-Host "Sorted on both properties with -Unique" -ForegroundColor Yellow
$objects | Sort-Object -Property Message,MachineName -Unique | Out-Host

Write-Host "Sorted on just Message with -Unique" -ForegroundColor Yellow
$objects | Sort-Object -Property Message -Unique | Out-Host

Write-Host "Sorted on just MachineName with -Unique" -ForegroundColor Yellow
$objects | Sort-Object -Property MachineName -Unique | Out-Host

输出:

Sorted on both properties with -Unique

Message MachineName
------- -----------
1       1          
1       2          
2       1          
2       2          
3       1          
3       2          
4       1          
4       2          
5       1          
5       2          


Sorted on just Message with -Unique

Message MachineName
------- -----------
1       1          
2       1          
3       1          
4       1          
5       2          


Sorted on just MachineName with -Unique

Message MachineName
------- -----------
1       1          
3       2  

来源: https://powershell.org/forums/topic/need-to-unique-based-on-multiple-properties/

答案 4 :(得分:3)

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

 $a | Get-Unique

答案 5 :(得分:3)

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

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

<强>输出:

Apples
Banana

这会使用Property参数首先Trim()字符串,因此会删除额外的空格,然后只选择-Unique值。

有关Sort-Object的更多信息:

Get-Help Sort-Object -ShowWindow

答案 6 :(得分:2)

您是否正在使用&#34; SORT -UNIQUE&#34;,&#34; SELECT -UNIQUE&#34;或者&#34; GET-UNIQUE&#34;从Powershell 2.0到5.1,给出的所有示例都在单列数组上。我还没有让它在具有多个列的Arrays中运行以删除重复行以在所述列中留下单个出现的行,或者开发替代脚本解决方案。相反,这些cmdlet仅返回在发生ONCE时出现奇异的数组中的行,并转储所有具有重复的内容。通常,我必须从Excel中的最终CSV输出中手动删除重复项以完成报告,但有时我希望在删除重复项后继续使用Powershell中的所述数据。

答案 7 :(得分:1)

使用我的方法,您可以完全删除重复的值,只剩下计数为1的数组中的值。目前尚不清楚这是否是OP真正想要的,但是我找不到此示例在线解决方案就在这里。

$array=@'
Bananna
Apple
Carrot
Pear
Apricot
Pear
Bananna
'@ -split '\r\n'

($array | Group-Object -NoElement | ?{$_.count -eq 1}).Name

答案 8 :(得分:0)

要从数组中获取唯一项并保留其顺序,您可以使用 .NET HashSet:

$Array = @(1, 3, 1, 2)
$Set = New-Object -TypeName 'System.Collections.Generic.HashSet[int]' -ArgumentList (,[int[]]$Array)

# PS>$Set
# 1
# 3
# 2

最适合包含大写和小写项目的字符串数组,您需要以不区分大小写的方式保留每个项目的第一次出现:

$Array = @("B", "b", "a", "A")
$Set = New-Object -TypeName 'System.Collections.Generic.HashSet[string]' -ArgumentList ([string[]]$Array, [StringComparer]::OrdinalIgnoreCase)

# PS>$Set
# B
# a

与其他类型一起按预期工作。

语法缩短,兼容 PowerShell 5.1 及更新版本:

$Array = @("B", "b", "a", "A")
$Set = [Collections.Generic.HashSet[string]]::new([string[]]$Array, [StringComparer]::OrdinalIgnoreCase)

$Array = @(1, 3, 1, 2)
$Set = [Collections.Generic.HashSet[int]]::new([int[]]$Array)