SPE - Rename duplicated name

function renameDuplicateItem($id){
$item = Get-Item -Path master: -ID $id
Write-Output "Check item $($item.Name)"
$index = 1
Get-ChildItem -Path $item.Parent.Paths.FullPath | Where-Object{ $_.ID.Guid -ne $id.Guid -and $_.Name -eq $item.Name } | Foreach-Object{
$newName = -Join($_.Name, " ", $index)
Write-Host "$($_.Name) will be changed to $newName"
$_.Editing.BeginEdit()
$_.Name = $newName
$_.Editing.EndEdit()
$index++
}
}

Get-ChildItem -Path "master:/sitecore/media library/Project/" -Recurse | Foreach-Object{
renameDuplicateItem $_.ID
}