PowerShell Function to Determine the Hyper-V Host for a VM

This week, I thought I would share a PowerShell function that I wrote to determine what Hyper-V host server a VM (virtual machine) resides on. In this scenario, you have no idea which Hyper-V host a VM resides on.

First off, let me say that this function is written a bit unorthodox.

Typically you’ll see functions written where each item in the ComputerName array is iterated through one at a time. Since this function uses the PowerShell remoting Invoke-Command cmdlet, it’s able to perform the task in parallel so why would anyone want to constrain it to only checking one computer at a time? Most commonly, this is done for error handling to determine if a computer is unreachable, if the credentials are invalid, and if the command generates an error such as the registry key not existing.

I started out first by using the built-in Error variable, clearing it and then iterating through it if any errors occurred. I decided that wasn’t necessarily a good idea since something else could potentially pollute the built-in variable so I decided to specify my own variable by using the ErrorVariable parameter. The function uses the ErrorAction parameter with the SilentlyContinue value. Using that particular option silences the interactive errors but still populates the variable that’s specified with ErrorVariable so I’m able to iterate through the errors and display warnings for systems whose results weren’t returned due to some sort of error.

I thought this solution was unique and I’m interested in knowing what you think about it.

The function shown in this blog article can be downloaded from my PowerShell repository on GitHub.



  1. Mike Shepard

    Kudos! I think we all need to be trying to use built-in array parameters (like you did for the ComputerName on Invoke-Command).

    Also, using ErrorVariable rather than clearing/looping through $Error is a good choice.

    Well done!

  2. wasserja

    Excellent! Thanks Mike for the simple and useful script. The code formatting is so readable.

  3. Vern Anderson (@Vern_Anderson)

    Actually if you use Get-VM the VM property “ComputerName” is the owning node. I know it’s dumb and the Hyper-V Team should not have named the property that but that is what the value of that property gives you.

    How would you get this ownernode if you could not log into each VM?

  4. Vern Anderson (@Vern_Anderson)

    $Nodes = Get-ClusterNode | Select-Object -ExpandedProperty Name
    foreach ($Node in $Nodes)
    Get-VM -ComputrName $Node | Format-Table Name,ComputerName -AutoSize

  5. Vern Anderson (@Vern_Anderson)

    # Last post was from memory
    ## Correcting my typos and pasting the script that actually does run
    $Nodes = Get-ClusterNode | Select-Object -ExpandProperty Name
    foreach ($Node in $Nodes)
    Get-VM -ComputerName $Node | Format-Table Name,ComputerName -AutoSize


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: