49

I'm trying to use Resize-VHD cmdlet, but this results in the following error:

PS> Resize-VHD -Path "C:\Container.vhd" -SizeBytes 20GB
    Error:
        Resize-VHD : The term 'Resize-VHD' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
        At line:1 char:1
        + Resize-VHD -Path "C:\Container.vhd" -SizeBytes 20GB
        + ~~~~~~~~~~
            + CategoryInfo          : ObjectNotFound: (Resize-VHD:String) [], CommandNotFoundException
            + FullyQualifiedErrorId : CommandNotFoundException

How do I fix that?

Codeguard
  • 1,039

2 Answers2

63

In case you have Windows 10 Home edition (so you have no Hyper-V at all), you can use alternative for Optimize-VHD cmdlet:

wsl --shutdown
diskpart
# open Diskpart in new window
select vdisk file="C:\WSL-Distros\…\ext4.vhdx"
attach vdisk readonly
compact vdisk
detach vdisk
exit

Thanks to @merkuriy for the tip.

Johny
  • 749
58

Turns out I needed to install Hyper-V features on Windows (even though I don't need Hyper-V itself, I use VHD as portable file container)

  1. Go to Control Panel | Programs and features | Turn windows features on or off
  2. Tick Hyper-V | Hyper-V Management tools | Hyper-V Module for Windows PowerShell
  3. When installed, reboot if asked
Codeguard
  • 1,039