PowerShell: Find similar filenames in a directory
Filter the $Files
array to exclude the current file when matching:
($Files | Where-Object {$_.FullName -ne $File.FullName}) -match $EpisodeNumber
Regarding the duplicates in the array the end, you can use Select-Object -Unique
to only get distinct entries.
Since you know how to get the episode number let's use that to group the files together.
$Files = Get-ChildItem -Path $Directory -Exclude '*.nfo','*.srt','*.idx','*.sub' | Select-Object FullName, @{Name="EpisodeIndex";Expression={ # We do not have to do it like this but if your detection logic gets more complicated then having # this select-object block will be a cleaner option then using a calculated property If ($_.BaseName -match 'S*(\d{1,2})(x|E)(\d{1,2})'){$Matches[0]}}}# Group the files by season episode index (that have one). Return groups that have more than one member as those would need attention.$Files | Where-Object{$_.EpisodeIndex } | Group-Object -Property EpisodeIndex | Where-Object{$_.Count -gt 1} | ForEach-Object{ # Expand the group members $_.Group # Not sure how you plan on dealing with it. }