Powershell script to search for similar filenames

Solution 1:

But I need to sort by unique and count them, maybe, before action.

You definitely want the Group-Object cmdlet!

As the name suggests, it... groups objects, based on some common property:

$filesByCommonBaseName = Get-ChildItem -Path "E:\test" -Filter "*.TIF" |Group-Object { $_.BaseName -replace '.{3}$' }

Now that you have them grouped correctly, you can start operating on them as such:

foreach($entry in $filesByCommonBaseName){
    Write-Host "Operating on files starting with $($entry.Name)"
    if($entry.Count -eq 4){
        # we found four matches, move them!
        $entry.Group |Move-Item -Destination $destinationDir -Force
    }
}