What is needed to use Vagrant with Hyper-V

I’ve asked this question first at What is needed to use Vagrant with Hyper-V - Super User

I’ve set up vagrant as described in the docs.

When running vagrant up I get the following message:

Bringing machine 'default' up with 'hyperv' provider...
==> default: Verifying Hyper-V is enabled...
The Hyper-V cmdlets for PowerShell are not available! Vagrant
requires these to control Hyper-V. Please enable them in the
"Windows Features" control panel and try again.

While I understand the message, I feel this is not the underlying issue. I did

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All

and the following reboot.

Get-VM lists all my machines. So I feel the Hyper-V cmdlets should be installed and usable.

This is running on Windows 10 20H2, Vagrant version is 2.2.14, Hyper-V PowerShell module version is 2.0.0.0

What am I missing here?

1 Like

The error is coming from \Vagrant\embedded\gems\2.2.14\gems\vagrant-2.2.14\plugins\providers\hyperv\scripts\check_hyperv.ps1 which essentially does Get-Command "Hyper-V\Get-VMSwitch" and if that fails reports the error.

Now, my system seems to be unable to load Hyper-V\Get-VMSwitch without some “help”:

❯ get-command "Hyper-V\Get-VMSwitch"
get-command : The term 'Hyper-V\Get-VMSwitch' 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
+ get-command "Hyper-V\Get-VMSwitch"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Hyper-V\Get-VMSwitch:String) [Get-Command], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException,Microsoft.PowerShell.Commands.GetCommandCommand

❯ get-command "Get-VMSwitch"

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Cmdlet          Get-VMSwitch                                       2.0.0.0    Hyper-V


❯ get-command "Hyper-V\Get-VMSwitch"

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Cmdlet          Get-VMSwitch                                       2.0.0.0    Hyper-V

1 Like

After much trying and checking other machines for cross-referenct it boiled down to Import-Module Hyper-V not working on my machine, while at the same time Get-VMSwitch would load the Hyper-V module.

This brought me eventually to $env:PSModulePath, which on two (!) of my machines looked like this:
image

(notice the System.String[] at the end) while on newly installed machines it contained %SystemRoot%\system32\WindowsPowerShell\v1.0\Modules (and not the System.String[] rubble)

So. Changing PSModulePath environment to look like this:
image

fixed the whole problem.

2 Likes

Thank you good sir! This was also a problem for me, was scratching my head for couple of days!
After replacing the System.String with %SystemRoot%\system32\WindowsPowerShell\v1.0\Modules vagrant up for hyperv is working for mew. Whew!
I wish I could buy you a beer!! Thank you!