Forum Discussion

Rich89's avatar
Rich89
Brass Contributor
Dec 09, 2021

Counting Matches in an Array

I am hoping someone can point me to documentation or explain how/why this is working in a script that I've written.  What I needed to do is make PowerShell create a report of any duplicated file in a...
  • AndySvints's avatar
    Dec 09, 2021

    Hello Rich89,

    Excerpt from -match operator documentation:

     

    ...When the input is a collection of values, the operators return any matching members. If there are no matches in a collection, the operators return an empty array.

     

    So in your specific use case:

     

    $howMany  = $files.hash -match $file.hash

     

     

    If there are identical files in $files.hash collection, your $howMany variable will contain collection of matching hashes.(in this case there will be at least 2 members).

     

    If there are no duplicates of specific file hash your $howMany variable will contain empty array.

     

    Reference: about_Comparison_Operators.MatchingOperators

    Hope that helps.

Resources