
Fotolia
Get to know the versatile Get-VM PowerShell cmdlet
The Get-VM PowerShell cmdlet obtains configuration properties of VMs running on a single local or remote Hyper-V server.
Hyper-V administrators can view and adjust a variety of VM configurations with the Get-VM PowerShell cmdlet. Microsoft...
Continue Reading This Article
Enjoy this article as well as all of our content, including E-Guides, news, tips and more.
put a lot of effort into refining PowerShell to improve management of local or remote servers. The PowerShell command line is available for most server roles, and Microsoft continues to develop new and enhance existing PowerShell cmdlets to minimize the time it takes to perform an operation via GUI.
Retrieving the list of Hyper-V servers
Before we get to the Get-VM PowerShell cmdlet, we need to retrieve the list of Hyper-V servers joined to an Active Directory domain and then save the output to a comma-separated values (CSV) file. To do so, execute the script shown below from a computer that has Active Directory PowerShell modules installed.
$ResultFile = "C:\Temp\HyperVServers.CSV"
Remove-item $ResultFile -ErrorAction SilentlyContinue
$STR = "Hyper-V Server, Operating System"
Add-Content $ResultFile $STR
$AllHyperVServers = Get-ADObject -Filter 'ObjectClass -eq
"serviceConnectionPoint" -and Name -eq "Microsoft Hyper-V"'-
ErrorAction SilentlyContinue
foreach($Item in $AllHyperVServers)
{
$Item.DistinguishedName
$DN1, $DN2, $DN3 = $Item.DistinguishedName.Split(",")
$RC1, $RC2 = $DN2.Split("=")
$DN1,$DN2 = $Item.DistinguishedName.split(",")
$ThisItem = Get-ADComputer -Id $RC2 -Properties *
$HyperVServerNow = $ThisItem.Name
$HyperVOSVersionNow = $ThisItem.OperatingSystem
$HyperVServerNow
$HyperVOSVersionNow
$STR = $HyperVServerNow+","+$HyperVOSVersionNow
Add-Content $ResultFile $STR
}
Once you have executed this script, the resulting file will list the Hyper-V server names and OS versions, as shown in Figure A.

Note that many of the PowerShell cmdlets have been designed to work with a specific version of Hyper-V OS. That's why it's necessary to keep the OS version value in the HyperVServers.CSV file.
Get-VM PowerShell cmdlet
The Get-VM PowerShell cmdlet offers useful properties other than just listing the standard VM configuration from a Hyper-V server. If you type Get-VM on a local Hyper-V host, the command will show you all VMs configured on that host, but if you need to get the VM information from a remote Hyper-V host, just specify the remote Hyper-V server name in the –ComputerName parameter as shown in the command below:
Get-VM –ComputerName <RemoteHyper-V Server Name>
ForEach loop mechanism
By running the command above, you can get a list of VMs from a single remote Hyper-V server, but what if you need to list VMs from more than one Hyper-V host? To list VMs and their configurations from multiple remote Hyper-V servers, all you need to do is use a ForEach loop as shown below:
$ServerFile = “C:\Temp\HyperVServers.CSV”
$CSV = Import-CSV $ServerFile
ForEach ($Item in $CSV)
{
$HyperVServerName = $Item.’Hyper-V Server’
$HyperVOS = $Item.’Operating System’
< Hyper-V PowerShell Command goes here >
}
As you can see, the PowerShell script above uses HyperVServers.CSV, which contains the list of Hyper-V servers and their OS versions that we retrieved. The script imports the HyperVServers.CSV file in a $CSV variable, and then each Hyper-V name and its OS version are captured in $HyperVServerName and $HyperVOS variables respectively. So, to execute the Get-VM command against each Hyper-V server mentioned in the HyperVServers.CSV file, the script should look like this:
$ServerFile = “C:\Temp\HyperVServers.CSV”
$CSV = Import-CSV $ServerFile
ForEach ($Item in $CSV)
{
$HyperVServerName = $Item.’Hyper-V Server’
$HyperVOS = $Item.’Operating System’
Get-VM –ComputerName $HyperVServerName
}
If you want to get the VM configuration from a Hyper-V OS of your choice, add an IF condition that checks the OS version of the host before executing the Get-VM command as shown in the italic text in the script below:
$ServerFile = “C:\Temp\HyperVServers.CSV”
$CSV = Import-CSV $ServerFile
ForEach ($Item in $CSV)
{
$HyperVServerName = $Item.’Hyper-V Server’
$HyperVOS = $Item.’Operating System’
IF ($HyperVOS –eq “Windows Server 2012”)
{
Get-VM –ComputerName $HyperVServerName
}
}
Get-VM properties
As stated earlier, the Get-VM PowerShell cmdlet offers a lot of other properties that can retrieve a specific type of information. By default, Get-VM without any parameters shows only VM name, VM state, CPU usage, assigned memory to VM and uptime information. If you need to know what other properties Get-VM has to offer, execute Get-VM | Get-Member from a local Hyper-V host. As you can see in Figure B, using Get-VM you can also list other information, such as when the VM was created, whether dynamic memory is enabled, which network adapters are connected to the VM and so on.

Let's say you want to know the dynamic memory status for all of the VMs configured on each Hyper-V server mentioned in the HyperVServers.CSV file and then generate the report in a CSV file. To accomplish this goal, run the following PowerShell script:
$ServerFile = "C:\Temp\HyperVServers.CSV"
$CSV = Import-CSV $ServerFile
$ReportFile = "C:\Temp\HyperVReport.CSV"
$STR = "Hyper-V Server, Command Status, VM Name, Dynamic Memory Status"
Add-Content $ReportFile $STR
ForEach ($Item in $CSV)
{
$HypervServerName = $Item.'Hyper-V Server'
$HyperVOS = $Item.'Operating System'
$ComStatus = "Ok"
$Error.Clear()
$RVM = Get-VM -ComputerName $HypervServerName | select-Object Name, DynamicMemoryEnabled
IF ($Error.Count -eq 0)
{
### START - Command Goes Here ####
ForEach ($ItemVM in $RVM)
{
$VMName = $ItemVM.Name
$DMStatus = $ItemVM.DynamicMemoryEnabled
$STR = $HypervServerName+",OK,"+$VMName+","+$DMStatus
}
### END - Command Goes Here ####
}
else
{
$ErrorNow = $Error[0].Exception.Message
$ComStatus = "Error Executing Command: Error: "+$ErrorNow
$STR = $HypervServerName+","+$ComStatus
Add-Content $ReportFile $STR
}
}
Once you have finished executing this script, the C:\Temp\HyperVReport.CSV file will generate a report, as shown in Figure C, that indicates whether dynamic memory is enabled on the VM.

Once you know the VM configuration you need to retrieve from one or more Hyper-V servers, pick a property of your choice and then plug it into the script shown above. You might also want to make some adjustments to your command in the ### START - Command Goes Here #### section.