Ok, got it.
Try something like this.
First try with 1 datastore and 1 VM.
Only remove the WhatIf when you are absolutely sure it is renaming the correct files with the correct name
$dsName = 'MyDS'$ds = Get-Datastore -Name $dsName
Get-VM -Name MyVM -PipelineVariable vm |
ForEach-Object -Process {
$path = $vm.ExtensionData.Summary.Config.VmPathName.Split('/')[0]
$folder = $path.Split(' ')[1]
$dsName = $folder.Split(' ')[0].Trim('[]')
New-PSDrive -Location $ds -Name DS -PSProvider VimDatastore -Root '/' | Out-Null
Get-ChildItem -Path "DS:/$folder" |
Where-Object { $_.Name -notmatch "^\." -and $_.Name.Split('.')[0].Split('-')[0].Split('_')[0] -cne $vm.Name } |
ForEach-Object -Process {
Write-Host "For VM $($vm.Name) the file $($_.Name) is inconsisten"
Rename-Item -Path "DS:\$folder\$($_.Name)" -NewName $vm.Name -WhatIf
}
Remove-PSDrive -Name DS -Confirm:$false
}