# changeGuessVmNameInVmm.ps1
# The following script prepend all VM's matching certain pattern
# This is useful to mark VM's to be deleted for ease of administration

$nameLike='test'
$prefix='_decom 02-01-2020_ '

$targetVms=Get-SCVirtualMachine|?{$_.Name -like "*$nameLike*"} 
foreach($targetVm in $targetVms){
    try{
        $oldName=$targetVm.Name
        $newName="$($prefix+$oldName)"
        $changed=Set-SCVirtualMachine -VM $targetVm -Name $newName
        if($changed.name -eq $newName){
            write-host "$oldName's name has been changed to $newName" -foregroundcolor Green
        }else{
            write-host "$oldName's name has NOT been changed to $newName" -foregroundcolor Red
        }
    }catch{
        write-warning $_        
    }    
}