Hyper-V PowerShell One-Line-Challenge
While digging through my notes I came across an exchange between a colleague and I - where we were challenging each other to come up with the most impressive (and functional) "Hyper-V PowerShell One Liner". Here is an example of a good one:
Get-Volume | ?{$_.DriveLetter -ne $null} | select @{N='Drive Letter';E={$_.DriveLetter}},@{N='Free Space (GB)';E={"{0:N2}" -f ($_.SizeRemaining / 1GB)}},@{N='Space Used By VHDs (GB)';E={$driveletter = $_.DriveLetter; "{0:N2}" -f ((Get-VMHardDiskDrive * | ? {$_.Path -match "^$driveletter" } | Get-VHD | Measure -sum FileSize | Select -exp sum)/1GB)}}
What does this one liner do? Well - let's run it and see:
If you pull it apart you can see that what it does is:
- Get all disks in the physical computer
- List the drive letters of each disk
- Report the free space on each disk (formatted to gigabytes)
- Finds all virtual hard disks that Hyper-V knows about on each disk and displays how much space they are using (formatted to gigabytes)
Very nice!
Cheers,
Ben