SOLVED

Counting Matches in an Array

%3CLINGO-SUB%20id%3D%22lingo-sub-3034946%22%20slang%3D%22en-US%22%3ECounting%20Matches%20in%20an%20Array%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3034946%22%20slang%3D%22en-US%22%3E%3CP%3EI%20am%20hoping%20someone%20can%20point%20me%20to%20documentation%20or%20explain%20how%2Fwhy%20this%20is%20working%20in%20a%20script%20that%20I've%20written.%26nbsp%3B%20What%20I%20needed%20to%20do%20is%20make%20PowerShell%20create%20a%20report%20of%20any%20duplicated%20file%20in%20a%20directory.%26nbsp%3B%20I've%20done%20this%20via%20hash%2C%20and%20the%20script%20works%20great.%26nbsp%3B%20I%20did%20it%20by%20compiling%20an%20array%20of%20all%20files%2C%20and%20then%20checking%20if%20the%20count%20of%20a%20hash%20is%20greater%20than%201%2C%20and%20if%20it%20is%20add%20that%20to%20a%20second%20array%20which%20is%20later%20used%20to%20generate%20a%20csv%20report.%26nbsp%3B%20What%20I%20want%20to%20have%20explained%20to%20me%20or%20to%20have%20someone%20point%20me%20to%20something%20that%20will%20explain%20it%20for%20me%20is%20this%20line%3A%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3CP%3Eforeach%20(%24file%20in%20%24files)%3CBR%20%2F%3E%7B%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%3CEM%3E%3CSTRONG%3E%24howMany%26nbsp%3B%20%3D%20%24files.hash%20-match%20%24file.hash%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3Bif%20(%24howMany.count%20-gt%201)%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%7B%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%24copiedDoc%20%2B%3D%20%24file%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%7D%3C%2FP%3E%3CP%3E%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20line%20that%20I%20have%20bolded%20there%2C%20why%20does%20that%20work%3F%26nbsp%3B%20Does%20PowerShell%20just%20know%20to%20make%20%24howMany%20an%20array%20because%20it's%20being%20set%20equal%20to%20an%20array%20of%20matches%20against%20the%20%24file%2C%20and%20so%20when%20you%20do%20the%20.count%20it%20works%3F%26nbsp%3B%20The%20reason%20I'm%20confused%20is%20I'm%20looking%20at%20%24howMany%20as%20a%20variable%2C%20not%20an%20array%20because%20I%20create%20blank%20arrays%20at%20the%20start%20of%20the%20script%20to%20fill%20in%20later%20and%20this%20isn't%20one%20of%20them%2C%20so%20it%20shouldn't%20be%20able%20to%20store%20more%20than%20one%20value%20in%20it...%20right%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI'm%20still%20pretty%20new%20to%20PowerShell%20scripting%2C%20and%20I%20apologize%20if%20this%20is%20a%20stupid%20question.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3034946%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EWindows%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3035623%22%20slang%3D%22en-US%22%3ERe%3A%20Counting%20Matches%20in%20an%20Array%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3035623%22%20slang%3D%22en-US%22%3EOops%2C%20link%20has%20been%20updated.%3CBR%20%2F%3EThank%20you.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3035453%22%20slang%3D%22en-US%22%3ERe%3A%20Counting%20Matches%20in%20an%20Array%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3035453%22%20slang%3D%22en-US%22%3EThat%20perfectly%20answers%20my%20question%20thank%20you.%20Your%20link%20appears%20to%20take%20me%20to%20a%20different%20page%20than%20intended%20I%20think%2C%20but%20your%20excerpt%20explains%20it.%20Thank%20you!%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3035451%22%20slang%3D%22en-US%22%3ERe%3A%20Counting%20Matches%20in%20an%20Array%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3035451%22%20slang%3D%22en-US%22%3E%3CP%3EHello%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1038301%22%20target%3D%22_blank%22%3E%40Rich89%3C%2FA%3E%2C%3C%2FP%3E%3CP%3EExcerpt%20from%20-match%20operator%20documentation%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E...%3CEM%3EWhen%20the%20input%20is%20a%20collection%20of%20values%2C%20the%20operators%20return%20any%20matching%20members.%20If%20there%20are%20no%20matches%20in%20a%20collection%2C%20the%20operators%20return%20an%20empty%20array.%3C%2FEM%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESo%20in%20your%20specific%20use%20case%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24howMany%20%20%3D%20%24files.hash%20-match%20%24file.hash%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20there%20are%20identical%20files%20in%20%3CSTRONG%3E%24files.hash%3C%2FSTRONG%3E%20collection%2C%20your%20%3CSTRONG%3E%24howMany%3C%2FSTRONG%3E%20variable%20will%20contain%20collection%20of%20matching%20hashes.(in%20this%20case%20there%20will%20be%20at%20least%202%20members).%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20there%20are%20no%20duplicates%20of%20specific%20file%20hash%20your%20%3CSTRONG%3E%24howMany%3C%2FSTRONG%3E%20variable%20will%20contain%20empty%20array.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EReference%3A%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fmodule%2Fmicrosoft.powershell.core%2Fabout%2Fabout_comparison_operators%3Fview%3Dpowershell-7.2%23matching-operators%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Eabout_Comparison_Operators.MatchingOperators%3C%2FA%3E%3C%2FP%3E%3CP%3EHope%20that%20helps.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Contributor

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 directory.  I've done this via hash, and the script works great.  I did it by compiling an array of all files, and then checking if the count of a hash is greater than 1, and if it is add that to a second array which is later used to generate a csv report.  What I want to have explained to me or to have someone point me to something that will explain it for me is this line:

foreach ($file in $files)
{

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

     if ($howMany.count -gt 1)

     {

          $copiedDoc += $file

     }

}

 

The line that I have bolded there, why does that work?  Does PowerShell just know to make $howMany an array because it's being set equal to an array of matches against the $file, and so when you do the .count it works?  The reason I'm confused is I'm looking at $howMany as a variable, not an array because I create blank arrays at the start of the script to fill in later and this isn't one of them, so it shouldn't be able to store more than one value in it... right?

 

I'm still pretty new to PowerShell scripting, and I apologize if this is a stupid question.

3 Replies
best response confirmed by Rich89 (Contributor)
Solution

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.

That perfectly answers my question thank you. Your link appears to take me to a different page than intended I think, but your excerpt explains it. Thank you!
Oops, link has been updated.
Thank you.