$SourceDirectory = ".\SOURCE"
$DestinationDirectory = ".\DEST"
$Directories = Get-ChildItem -Path $DestinationDirectory -Directory
$Files = Get-ChildItem -Path $SourceDirectory -File -Recurse
foreach($Dir in $Directories) {
foreach($File in $Files) {
if($File.Name -like "*$($Dir.Name)*") {
if(Test-Path "$($Dir.FullName)\$($File.Name)") {
$Items = Get-ChildItem -Path "$($Dir.FullName)" -File
if($Items) {
$NewFileName = "$($File.Name.Split('.')[0])$($Items.Count)$($File.Extension)"
Move-Item -Path "$($File.FullName)" -Destination...