Windows 2012 R2 : Script to Rename Computer Based Upon Hyper-V Guest Name

I came across the following TechNet post recently which is useful for deploying Hyper-V machines and automating machine renaming to be in-line with the names given to you Hyper-V guests:

However, I ran into issues when testing on a Windows 2012 R2 Hyper-V Server and Windows 2012 R2 guest, so modified the rename component as follows – ensure you run from within the guest Operating System:

$virtualmachinename= (Get-ItemProperty 'HKLM:\software\microsoft\virtual machine\guest\parameters').virtualmachinename 
Rename-Computer -NewName $virtualmachinename.ToUpper()