Powershell-File names in a directory with same letters

657 views Asked by At

I need to write a powershell script, which list file names that contains the same letters, only difference is the sort of the letters.

My first oppinion is to sort the letters in alphabet, and if it fit, then they match, but i need some help for do that

Get-ChildItem $path | foreach  {$i=1}`
{  
        $asd=$_ | sort-object
        Get-ChildItem $path | foreach  {$i=1}`
        {  
            $wasd=$_ | sort-object              
            if($asd -eq $wasd)
            {
                Write-Host $_
            }

        }
}

This files match for my criteria: asd.txt, dsa.txt, because contains same letters

2

There are 2 answers

4
Mike Shepard On BEST ANSWER

I think this is doing what you want.

function get-Stringcharacters {
param($string)
  [char[]]$string | sort-object
}

dir $path | group-object @{E={get-Stringcharacters $_.Name}} | 
          where-object {$_.Count -gt 1} | 
          select-object -ExpandProperty Group |
          foreach { write-host $_.Name }
1
KevinD On

gci | % { ($.BaseName.ToString().toCharArray() | Sort) -join ''} | Group | ? { $.Count -gt 1 } | Select Name