cynicalswan77
cynicalswan77

Reputation: 289

Sort-Object doesn't remove duplicate strings - Powershell

I have an array of paths and an array of exes I am matching against and getting the name of any exes that match under per path. I know for a fact there are not duplicate entries, and that the found exes only exist under just ONE of the paths in the array. But, when I do Sort-Object -Unique, there are duplicates and they are not removed.

Code:

$found_paths =@("C:\Program Files\Microsoft Office Servers\OFFICE15", "C:\Program Files\Microsoft Office\Office15");

$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
   }
} 

Output:

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

Upvotes: 0

Views: 109

Answers (1)

Santiago Squarzon
Santiago Squarzon

Reputation: 60110

This is because you are getting the same binary from a different location ($path) and your sort statement was inside a loop, where files are already unique.

$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

Upvotes: 4

Related Questions