0

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 
2
  • double check that there are no trailing whitespace chars. Commented Jan 12, 2022 at 0:15
  • To see what is happening, change $found_files.Name to $found_files.FullName. Commented Jan 12, 2022 at 2:41

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.