44

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?

2
  • 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
    Commented Jan 17, 2020 at 18:57
  • to mount a vhd you can use diskpart. Commented Feb 19, 2021 at 2:58

2 Answers 2

53

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
5
  • 11
    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.
    Commented Jul 17, 2018 at 22:54
  • Can we at least enable this from Powershell?
    – not2qubit
    Commented Jan 17, 2020 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
    Commented Jan 17, 2020 at 19:22
  • This is incorrect, I do have Hyper-V on my Windows 8.1
    – Codeguard
    Commented Jan 18, 2020 at 19:50
  • 4
    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).
    – joshua
    Commented Apr 19, 2021 at 4:26
50

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.

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. Commented Nov 8, 2022 at 10:15
  • wsl --shutdown, not commonly mentioned but fixes many issues
    – J. Doe
    Commented Sep 26, 2023 at 17:22
  • 1
    In case anyone is silly like me and read through the instructions too quickly, that detach vdisk step is *critically important* . I had to re-open diskpart, re-select the disk, and detach, before I could start WSL again. 10/10 instructions though, worked first try, gained me back 35GB of disk space.
    – JMA
    Commented Apr 15 at 7:24
  • You might also want to run sudo fstrim / first if the VHD is a Linux image
    – Mark
    Commented Jun 17 at 14:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .