33

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,053
  • 1
  • 9
  • 11
  • 1
    LoL! Another Masochistic Windowism! Not making basic Window functionality available, even though all other commands (via CMD) and GUI is available, but not the most basic powershell stuff like mounting your VHD! – not2qubit Jan 17 '20 at 18:57
  • to mount a vhd you can use diskpart. – Pedro Lobito Feb 19 '21 at 02:58

2 Answers2

45

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,053
  • 1
  • 9
  • 11
  • 8
    Well, I had to install the `Hyper-V Platform | Hyper-V Services` part, too. (Which requires a reboot.) `Resize-VHD` finally started working only after that. Note: I was worried (as I've seen pranks like that from MS, and others...), but `Hyper-V Services` fortunately does not require a Hyper-V capable hardware. – Sz. Jul 17 '18 at 22:54
  • Can we at least enable this from Powershell? – not2qubit Jan 17 '20 at 18:58
  • This doesn't work for Windows 8.1 as the `Hyper-V` features are not available. Is there a ways to install this by side loading? – not2qubit Jan 17 '20 at 19:22
  • This is incorrect, I do have Hyper-V on my Windows 8.1 – Codeguard Jan 18 '20 at 19:50
  • 3
    In case anyone can't find Hyper-V like I couldn't, you can only have Hyper-V if you're on Windows 10 Enterprise, Pro, or Education ([source](https://docs.microsoft.com/en-us/virtualization/hyper-v-on-windows/quick-start/enable-hyper-v)). – joshua Apr 19 '21 at 04:26
31

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
  • 429
  • 4
  • 4
  • I do have Windows Enterprise, but for some reason, Windows refused to install the component of Hyper-V for PowerShell, so I used this method, and it totally worked. – Oleksandr Novik Nov 08 '22 at 10:15