Hyper-v VM's hostname in Registry is no longer accurate

On my Windows 10 Pro Hyper-V, I have a Server 2019 VM whose hostname is the default WIN-MUMBOJUMBO. On searching the Intertubes for a possible solution to find this hostname ‘scriptually’, I came across this solution, which unfortunately no longer works:

(Get-Item “HKLM:\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters”).GetValue(“Hostname”)

When I run the above line, I get a different value for ‘Hostname’ than the WIN-MUMB…

So, is there another way to determine PowerShell scriptually the hostname of the VM(s). I need this so that I can then run Configurations on the different VMs I intend to create on Hyper-V.

Any help is appreciated.

# Get remote vm name from environment variables list
Invoke-Command -ComputerName "WIN-MUMBOJUMBO" -ScriptBlock {$env:COMPUTERNAME}

Thanks for the reply.

Your solution will work if I am able to know WIN-MUMBO… ahead of time. For example, I have a farm of new VMs I have deployed whose default “WIN-Mumbo…” default computernames I don’t know yet. I want to be able to get these default computernames so that I can do what you suggested to change their names. Conceptually this is what I am looking for:

$oldComputerName = Some-FunctionToGetVMsComputerName

$oldComputerName | Rename-Computer -NewName “Kangaroo”

Hope this helps clarify my request. Thank you.

 

Well I think I may have found the solution after all.

After reading 'random commandline’s suggestion on Invoke-Command, I looked it up and apparently there is a VMName parameter for Invoke-Command, so I was able to accomplish what I wanted:

Invoke-Command -VMname $vmName -ScriptBlock { Rename-Computer -NewName ‘Kangaroo’ -Restart }

Thank you.