排序对象不会删除重复的字符串 - Powershell



我有一个路径数组和一个与之匹配的exe数组,并获取每个路径下匹配的任何exe的名称。我知道事实上没有重复的条目,并且找到的exe只存在于数组中的一个路径下。但是,当我执行Sort-Object -Unique时,存在重复项,并且它们不会被删除。

代码:

$found_paths =@("C:Program FilesMicrosoft Office ServersOFFICE15", "C:Program FilesMicrosoft OfficeOffice15");
$exes = @("MSOCF.DLL", "access.exe", "word.exe", "wordCnv.exe", "WordViewer.exe", "Excel.exe", "ExcelCnv.exe", "ExcelViewer.exe", "PowerPoint.exe", 
"PowerPointViewer.exe", "PowerPointCnv.exe", "Publisher.exe", "Project.exe", "OneNote.exe", "InfoPath.exe Groove.exe", "FrontPage.exe", 
"SharePointDesigner.exe", "Visio.exe", "VisioViewer.exe", "Lync.exeOutlook.exe", "WINPROJ.EXE");
foreach($path in $found_paths)
{
foreach($exe in $exes)
{
$found_files = Get-Item ([System.IO.Path]::Combine($path, $exe)) -EA Ignore;
$found_file = $found_files.Name | Sort-Object -Unique;
$found_file
}
} 

输出:

MSOCF.DLL
WINPROJ.EXE
MSOCF.DLL
WINPROJ.EXE 

这是因为您从不同的位置($path(获取相同的二进制文件,并且您的排序语句位于循环中,其中的文件已经是唯一的。

$allFiles = foreach($path in $found_paths)
{
foreach($exe in $exes)
{
Get-Item ([System.IO.Path]::Combine($path, $exe)) -EA Ignore
}
}
$allFiles.Name | Sort-Object -Unique

最新更新