1

My one liner looks like:

get-vm vm* |select name, numcpu,memoryGB,@{e={$_|get-harddisk |select capacityGB};l="disk"}

My output looks like:

Name            NumCpu MemoryGB disk
----            ------ -------- ----
vm1             4      96       {@{CapacityGB=60}, @{CapacityGB=300}, @{CapacityGB=200}}
vm2             8      98       {@{CapacityGB=50}, @{CapacityGB=450}}
vm3             8      96       {@{CapacityGB=200}, @{CapacityGB=50}, @{CapacityGB=300}}
vm4             2      12       {@{CapacityGB=100}, @{CapacityGB=50}}

How can I clean up that output to only show the disk size numbers and not all the @{capacityGB= }?

1
  • 1
    What output were you expecting? (Hint: How should PowerShell output when there is more than one disk?) Commented Feb 22, 2017 at 20:58

2 Answers 2

3

Use Select-Object -ExpandProperty to extract the value of the property. That will remove @{CapacityGB= which is shown because you have an array of objects with a CapacityGB-property. Ex:

Get-VM vm* | Select-Object Name, NumCPU,MemoryGB,@{l="Disk";e={$_ | Get-HardDisk | Select-Object -Expand CapacityGB}}

Name NumCPU MemoryGB Disk          
---- ------ -------- ----          
vm1       4       96 {60, 300, 200}
vm2       8       98 {50, 450}

To remove the remaining { } which represents an array of values, you can combine them to a string:

Get-VM vm* | Select-Object Name, NumCPU,MemoryGB,@{l="Disk";e={($_ | Get-HardDisk | Select-Object -Expand CapacityGB) -join ', '}}

Name NumCPU MemoryGB Disk        
---- ------ -------- ----        
vm1       4       96 60, 300, 200
vm2       8       98 50, 450

Or if you only want this for console output, you could add linebreaks, but this requires Format-Table -Wrap to display properly in PowerShell

#Output from Format-Table is not exportable. It just results in formatdata-objects
Get-VM vm* |
Select-Object Name, NumCPU, MemoryGB, @{l="Disk";e={($_ | Get-HardDisk | Select-Object -Expand CapacityGB) -join "`n" }} |
Format-Table -Wrap

Name NumCPU MemoryGB Disk      
---- ------ -------- ----      
vm1       4       96 60        
                     300       
                     200       
vm2       8       98 50        
                     450 
Sign up to request clarification or add additional context in comments.

Comments

1

The immediate fix is to use select -ExpandProperty capacityGB:

get-vm vm* |select name, numcpu,memoryGB,@{e={$_|get-harddisk |select -ExpandProperty capacityGB};l="disk"}

This would result in something like:

Name            NumCpu MemoryGB disk
----            ------ -------- ----
vm1             4       96      {60, 300, 200}

-ExpandProperty means that only the specified property's value should be returned, not a custom object that has the specified property, which is what something like @{CapacityGB=60} represents in your output.

Note that PowerShell:

  • uses enclosing { ... } to represent an array-valued property

  • with the individual elements represented as they would be if you cast them to [string] (or used them in a double-quoted string).

If the output is for display only, and you want different formatting, build your own output string inside the script block, as shown in Frode F.'s helpful answer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.