PowerShell one-liner for virtual disk analysis

I needed to look at my virtual machines & their disk sizes – with Windows 2012 R2 upgrade in the works I need to do a bit more tidy up

I found two cmdlets in the Hyper-V module:

get-vmharddiskdrive – can be related to the virtual machine but doesn’t give a size

get-vhd – expects a path to the VHD file

Luckily get-vmharddiskdrive outputs the path. This gives me a nice pipeline:

Get-VM |
Get-VMHardDiskDrive |
Get-VHD |
select Path, @{N=’Size’; E={[math]::Round(($_.FileSize / 1gb), 2) }} |
sort  -Descending

Get the VMs pipe through get-vmharddiskdrive  and get-vhd  then select and sort and you’re done.

I always break my pipelines at a pipe symbol – it acts as a line continuation in the console and ISE so anything thing else is just extra unnecessary work

This entry was posted in Hyper-V, PowerShell V3, Windows Server 2012. Bookmark the permalink.

1 Response to PowerShell one-liner for virtual disk analysis

  1. psjjw says:

    I always break my pipelines at a pipe symbol, and indent all but the first line of the pipeline. This way every oneliner is a paragraph in my script. good for readabitlity.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s