Creating calculated properties
You can use the Select-Object
cmdlet to select certain properties of the objects that you want to return. For example, you can use the following code to return the name and the used space, in GB, of your virtual machines:
PowerCLI C:\> Get-VM | Select-Object -Property Name,UsedSpaceGB
But what if you want to return the used space in MB? The PowerCLI VirtualMachineImpl
object has no UsedSpaceMB
property. This is where you can use a calculated property. A calculated property is a PowerShell hash table with two elements: Name
and Expression
. The Name
element contains the name that you want to give the calculated property. The Expression
element contains a scriptblock with PowerCLI code to calculate the value of the property. To return the name and the used space in MB for all your virtual machines, run the following command:
PowerCLI C:\> Get-VM | >> Select-Object -Property Name, >> @{Name="UsedSpaceMB";Expression={1KB*$_.UsedSpaceGB}} >>...