Skip to content

Get-HyperVVMInventoryReport

Hyper-V: Generates a consolidated virtual machine inventory report

#Requires -Version 5.1
#Requires -Modules Hyper-V

[CmdletBinding()]
Param (
    [string]$ComputerName = "localhost",

    [PSCredential]$Credential,

    [ValidateSet('All', 'Running', 'Off', 'Stopping', 'Saved', 'Paused', 'Starting', 'Reset', 'Saving', 'Pausing', 'Resuming',
        'FastSaved', 'FastSaving', 'RunningCritical', 'OffCritical', 'StoppingCritical', 'SavedCritical', 'PausedCritical',
        'StartingCritical', 'ResetCritical', 'SavingCritical', 'PausingCritical', 'ResumingCritical', 'FastSavedCritical',
        'FastSavingCritical', 'Other')]
    [string]$State = "All"
)

Process {
    try {
        $params = @{
            'ComputerName' = $ComputerName
            'ErrorAction'  = 'Stop'
        }
        if ($Credential) { $params.Add('Credential', $Credential) }

        $vms = Get-VM @params

        if ($State -ne "All") {
            $vms = $vms | Where-Object { $_.State -eq $State }
        }

        $results = foreach ($vm in $vms) {
            [PSCustomObject]@{
                VMName           = $vm.Name
                State            = $vm.State
                CPUUsage         = $vm.CPUUsage
                MemoryAssignedMB = [math]::Round($vm.MemoryAssigned / 1MB, 2)
                MemoryDemandMB   = [math]::Round($vm.MemoryDemand / 1MB, 2)
                Status           = $vm.Status
                Uptime           = $vm.Uptime
                Generation       = $vm.Generation
                Version          = $vm.Version
                ComputerName     = $vm.ComputerName
            }
        }

        Write-Output ($results | Sort-Object VMName)
    }
    catch {
        throw
    }
}

Specifies the name of the Hyper-V host. Defaults to the local machine.

Specifies the credentials to use for the remote connection.

Optional. Filters virtual machines by their current operational state.

An interactive directory of PowerShell scripts.