WavebreakmediaMicro - Fotolia


Use this PowerShell cmdlet to retrieve Hyper-V guest OS info

You can't get detailed information about a Hyper-V guest OS with the Get-VM cmdlet; you have to use the Invoke-Command cmdlet to run the Get-CimInstance cmdlet against a VM instead.

Experienced admins know that PowerShell is Microsoft's preferred management tool. As such, the vendor has created...

a number of PowerShell cmdlets for managing Hyper-V VMs. Even so, gathering information from a Hyper-V guest OS often requires a roundabout approach.

The Get-VM cmdlet

The primary PowerShell cmdlet for gathering information about a VM is Get-VM. By itself, this cmdlet shows you a few basic statistics pertaining to every VM on a given host server.

If you need more detailed information about a specific VM, you can append the VM name and the Select-Object * cmdlet. This will lead PowerShell to display a wealth of information about the VM, as shown in Figure A.

The Get-VM cmdlet
Figure A. The Get-VM cmdlet can display a considerable amount of information about a VM.

As you look at the figure above, you'll notice that the Get-VM cmdlet provides only VM-level information. For example, the cmdlet displays information about the hardware that has been allocated to the VM, the VM's checkpoints and even its replication health, but the cmdlet doesn't provide any information whatsoever on the Hyper-V guest OS.

Invoke-Command and Get-CimInstance

PowerShell has a few different ways to get information pertaining to a Hyper-V guest OS. The method that I like to use involves two basic steps. The first involves using the Invoke-Command cmdlet to run a command against a remote system. In this case, the remote system is the VM about which you want to learn more.

The second step in deriving information about the VM's OS is to use the Get-CimInstance cmdlet to display information about the Hyper-V guest OS. Let's start by taking a look at the Get-CimInstance cmdlet, and then I will show you how to direct the cmdlet to a VM.

Let's suppose that my local server is running Windows, but I know nothing else about the OS. I can get some basic information by running this command:

Get-CimInstance -ClassName Win32_OperatingSystem

When I run the command listed above, Windows displays some basic information, such as the operating system's directory, the name of the organization, the build number, the registered user, the serial number and the version number. You can see an example of this in Figure B.

The Get-CimInstance cmdlet
Figure B. The Get-CimInstance cmdlet displays basic OS information.

As helpful as that command might be, it fails to display some of the most basic information, such as the OS version. By looking at the figure above, you would be hard pressed to tell if this machine was running Windows Server 2012 or Windows 10. Fortunately, the Get-CimInstance command can display additional information.

If you want to see everything that the Get-CimInstance cmdlet knows about an OS, then use this command:

Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object *

This command reveals far more information, as shown in Figure C.

Additional OS info
Figure C. The Get-CimInstance cmdlet can retrieve plenty of OS-level information.

Of course, the information shown in the figure above can be a bit overwhelming. If you want to narrow things down a bit, you can replace the * at the end of the command with the names of the items you want to display. For example, you might use a command like this:

Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object Caption, CSName, OSArchitecture

If you look at Figure D, for example, you can tell that this machine has a computer name of Hyper-V-4, and that it's running a 64-bit edition of Windows Server 2012 R2 Datacenter Edition.

Granular OS info
Figure D. The Get-CimInstance cmdlet can display granular information about an OS.

What if you wanted to run this same command against a VM that is running on this host? As was previously mentioned, one option is to use the Invoke-Command cmdlet to remotely connect to the VM. If, for example, I wanted to run the previously listed command against a VM named DC, then I could use these PowerShell cmdlets:


Invoke-Command -ComputerName 'DC' -Credential $Cred -ScriptBlock {Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object Caption, CSName, OSArchitecture}

The first line of code prompts you to enter the VM's credentials and then writes those credentials to a variable named $Cred. The second line of code establishes remote connectivity to the VM, and then displays the information you requested. You can see an example of this in Figure E.

Displayed OS info
Figure E. I have retrieved OS information from a VM.

One thing that's worth noting is that the computer name shown in the command above refers to the computer name that has been assigned to the OS. It doesn't refer to the VM name that's displayed within the Hyper-V Manager.

Dig Deeper on Microsoft Hyper-V management