如何从PowerShell数组中删除重复项?
$a = @(1,2,3,4,5,5,6,7,8,9,0,0)
如何从PowerShell数组中删除重复项?
$a = @(1,2,3,4,5,5,6,7,8,9,0,0)
使用Select-Object
(别名为select
)和 -Unique
开关;例如:
$a = @(1,2,3,4,5,5,6,7,8,9,0,0)
$a = $a | select -Unique
另一个选项是使用Sort-Object
(其别名为sort
,但仅适用于Windows),并带有-Unique
开关,将排序和去重组合在一起:
$a | sort -unique
@("B", "a", "A") | sort -unique
会产生 A B
。 - roxton如果您希望完全防弹,这是我的建议:
@('Apples', 'Apples ', 'APPLES', 'Banana') |
Sort-Object -Property @{Expression={$_.Trim()}} -Unique
输出:
Apples
Banana
使用Property
参数首先对字符串进行Trim()
处理,从而删除额外的空格,然后仅选择-Unique
值。
有关Sort-Object
的更多信息:
Get-Help Sort-Object -ShowWindow
$a | sort -unique
这个方法不区分大小写,因此可以删除具有不同大小写的重复字符串。解决了我的问题。
$ServerList = @(
"FS3",
"HQ2",
"hq2"
) | sort -Unique
$ServerList
以上输出内容为:
FS3
HQ2
以下是如何从具有两个或更多属性的数组中获取唯一项。排序是关键,确保正确工作。否则,只会返回一个项目。
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
Source: https://powershell.org/forums/topic/need-to-unique-based-on-multiple-properties/
通过我的方法,您可以完全删除重复值,只留下数组中仅出现一次的值。不过,不清楚这是否是原始问题的实际需求,但我无法在网上找到此解决方案的示例,因此在此提供。
$array=@'
Bananna
Apple
Carrot
Pear
Apricot
Pear
Bananna
'@ -split '\r\n'
($array | Group-Object -NoElement | ?{$_.count -eq 1}).Name
($properties | Group-Object -NoElement).Name | Get-Unique
。 - az1d$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)
select -Unique
不区分大小写。
sort -Unique
可以按原始顺序排序,这可能是你想要的。($properties | Group-Object -NoElement).Name | Get-Unique
SORT -UNIQUE
,SELECT -UNIQUE
还是 GET-UNIQUE
,所有给出的示例都是针对单列数组的。我尝试过在多列数组中实现去除重复行以保留跨这些列的单个行的唯一实例,但尚未成功,也没有开发出替代的脚本解决方案。相反,这些 cmdlet 只返回了在数组中出现一次的行,并删除了所有具有重复项的行。通常情况下,我需要在 Excel 的最终 CSV 输出中手动删除重复项才能完成报告,但有时我希望在删除重复项后继续使用 Powershell 中的数据。
Get-Unique
(或gu
)。 - Joeyselect -u
,但在代码中编写时,建议使用完整的PS措辞:Select-Object -Unique
。 - papo