Get-VM (Virtual Machines) with Custom Property attributes

Here is short script how to list all VMs (or hosts or other objects) from VMM with Custom Properties. You can also export such table to CSV format to import it in Excel and do some calculations.

When you want to list Custom Properties you have to use this CMDlet:

Get-SCCustomProperty | select Name, Members | Sort-Object Name

Name             Members
----             -------
CustApp          {}
Custom1          {VM, Template, VMHost}
Custom10         {VM, Template, VMHost}
Custom2          {VM, Template, VMHost}
Custom3          {VM, Template, VMHost}
Custom4          {VM, Template, VMHost}
Custom5          {VM, Template, VMHost}
Custom6          {VM, Template, VMHost}
Custom7          {VM, Template, VMHost}
Custom8          {VM, Template, VMHost}
Custom9          {VM, Template, VMHost}
Location         {VM, Template, VMHost, HostCluster...}
VMEndDate        {VM, Template}

And then you can list Virtual Machines. In this script we will use PSCustomObject to collect all the ingratiation and present it in tabular style.

$vmCPs = @()

# List all Custom Properties
$CPs = Get-SCCustomProperty | Where {$_.Members -like "VM"} | Sort-Object Name

# Custom Property filtering
# $CPs = Get-SCCustomProperty | Where {($_.Members -like "VM") -and ($_.Name -like "Custom1")} | Sort-Object Name

# List all VMs
$Members = Get-VM | Sort-Object Name

# VMs filtering
# $Members = Get-VM | Where {$_.Name -like "Servers*"} | Sort-Object Name

foreach($membObj in $Members) {
    "Object: $($membObj.Name)"
    $custObj = [PSCustomObject] @{}
    Add-Member -InputObject $custObj -MemberType NoteProperty -Name "Name" -Value $

    # You can also add some additional data to the table, ie. CPU Count and Memory
    # Add-Member -InputObject $custObj -MemberType NoteProperty -Name "CPUCount" -Value $membObj.CPUCount
    # Add-Member -InputObject $custObj -MemberType NoteProperty -Name "Memory" -Value $membObj.Memory    

    # Here you list all the Custom Properties
    foreach($cp in $CPs) {        
        $cpValue = Get-SCCustomPropertyValue -InputObject $membObj -CustomProperty $cp
        Add-Member -InputObject $custObj -MemberType NoteProperty -Name $($ -Value $($cpValue.value)

    $vmCPs += $custObj

$vmCPs | Format-Table -AutoSize

# List all columns
# $vmCPs | select * 

If you want to export Hosts with Custom Properties you have to change those lines:

$CPs = Get-SCCustomProperty | Where {$_.Members -like "VMHost"} | Sort-Object Name
$Members = Get-SCVMHost | Sort-Object Name

To export this report (table) to CSV you can use Export-Csv CMDlet:

$vmCPs | Export-Csv -Path "VMs_with_CP.csv" -NoTypeInformation -Encoding Default

If you have any questions or suggestions pleas write it down below in comments.

Post author

Leave a Reply