Renaming a Group of VMs with PowerShell

When I run my Hyper-V host I often import a group of VMs and I like to keep things ordered. I do this using the name of the VM and I append a project name to the start of each VM name. I got bored of selecting each VM and hitting F2 so here’s my quick way:

$servername = "server"
$VMNamePatterns="ImportPattern-"
$ProjectName ="ProjectPattern-"

$VMs = Get-VM | where vmname -like $VMNamePatterns*
foreach ($item in $VMs)
{
$ItemName = $item.Name
$ItemNameNoProject = $ItemName.Split("-")[3,4] -join "-"
$NewVMName = "$ProjectName-$ItemNameNoProject"
Set-VM -name $item.name -NewVMName $NewVMName
}

The script is somewhat interesting it looks at the VMs that have been imported, pattern matches the name they’re imported with (my vms are all named “Domain-ROLE”) then renames the VM.

What do you think?