Friday, May 16, 2014

Deleting orphaned Hyper-V disks from a server


I have a tame Powershell expert on hand, and the problem with Hyper-V is the standard GUI does not delete the disk when you delete a Virtual Machine, this can lead to a bunch of ophan VHD/VHDX files on the server. This script must be run on the server and will put out the VM drive path, the VMs that are on the server, the VHX/VHDX’s and will allow to see or delete them.

NOTE NOTE NOTE: This will delete valid snapshots… No I did not have any … Just a warning.

To view them run:

Delete-OphanedDisks.ps1 –whatif

To delete them run:


And follow the confirmation prompts.


PS: SCVMM does not have this problem. Cheers.


Script below, written by Peter Bertok





[string[]]$Extensions = @( '*.vhdx', '*.vhd' )



$disks = @( Get-VM | Get-VMHardDiskDrive | select -ExpandProperty Path )

$store = (Get-VMHost).VirtualHardDiskPath

If ( -not ( Test-Path $store ))


Throw "Cannot find default VM disk path: $store"


$List = @( $Extensions | `

ForEach-Object { dir -Path:$store -Filter $_ } | `

Select-Object -ExpandProperty FullName | `

Where-Object { $_ -notin $disks } | `

Select-Object -Unique )

If ( $Force )


$List | del -Force:$Force -Confirm:$False -WhatIf:$False


Else #If ( $PSCmdlet.ShouldProcess( "$($List.Length) Files", "Delete" ))


$List | del -Confirm:$ConfirmPreference



No comments:

Blog Archive