23

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

2 Answers 2

35

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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.