How to Remove VHD?

My question is in regards to removing a VHD using Remove-VMHardDiskDrive.  What is the correct syntax for the -VMHardDiskDrive<HardDiskDrive[]> parameter? I tried the name, drive letter, and number, but none worked.

Here are the steps I used to create the drive in case that aids in resolving the issue.

I created a drive using New-VHD. I mounted it with Mount-VHD; Get-Disk shows it as Number 6. I partitioned it and assigned a drive letter (it shows as J) Then I used Format-Volume -DriveLetter J -FileSystem NTFS -Full -Force and that worked.  Then I dismounted the drive, then connected the drive to a Linux guest in Hyper-V. I

I now want to remove the drive, but can't get the right syntax for Remove-VMHardDiskDrive. PowerShell keeps complaining about the VMHardDrive parameter:

PS C:\Windows\system32> Remove-VMHardDiskDrive -VMName CentOS_6.6_VM -VMHardDiskDrive MyDrive.vhdx -WhatIf
Remove-VMHardDiskDrive : Cannot bind parameter 'VMHardDiskDrive'. Cannot convert the "MyDrive.vhdx" value of type
"System.String" to type "Microsoft.HyperV.PowerShell.HardDiskDrive".
At line:1 char:63
+ Remove-VMHardDiskDrive -VMName CentOS_6.6_VM -VMHardDiskDrive MyDrive.vhdx -WhatIf
+                                                               ~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Remove-VMHardDiskDrive], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.HyperV.PowerShell.Commands.RemoveVMHardDiskDriveCommand

Any help is appreciated.

February 7th, 2015 1:15am

The key in the error is here:  Cannot convert the "MyDrive.vhdx" value of type "System.String" to type "Microsoft.HyperV.PowerShell.HardDiskDrive".

You are passing the name of the VHD ( a string ) and the cmdlet expects a VHD object.

You need to:  $vhd = Get-VMHardDiskDrive <selection parameter>And then use -VMHardDiskDrive $vhd

Free Windows Admin Tool Kit Click here and download it now
February 7th, 2015 2:25am

Thanks! Never thought of that.
February 7th, 2015 4:04am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics