Hyper-V administrators rely on remote management tools, such as Remote Desk Protocol or Virtual Machine Connect, if they need to execute PowerShell commands inside a Hyper-V VM. Although you can use the PowerShell Remoting feature to connect to a VM running on a Hyper-V host and execute commands remotely, this requires a working network connection, open network ports between the source and destination, and a series of PowerShell remote commands.

Windows Server 2016 Hyper-V hosts will help Hyper-V administrators solve common VM management problems. Starting with Windows Server 2016, you will be able to interact with VMs running on a Windows Server 2016 Hyper-V host in an easier, more reliable way. Before Windows Server 2016, a network connection and a modifying firewall configuration were necessary to run PowerShell commands inside a VM. Using the PowerShell Direct feature of Windows Server 2016, you can execute Windows PowerShell commands inside a VM from the Hyper-V host operating system (OS) without any network connection or modifying the organizational security policies.

There are a few prerequisites for using the PowerShell Direct feature. First, VMs and the Hyper-V host must be running Windows 10 or Windows Server 2016. Next, you must make sure that you are logged on to the Windows Server 2016 Hyper-V host using Hyper-V administrator. All VMs must run on the "local" Hyper-V host and, finally, PowerShell must be installed in the VM.

It is important to note that PowerShell Direct is enabled by default on Windows Server 2016 Hyper-V hosts. The hypervisor code in Windows Server 2016 has been modified to include the PowerShell Direct feature, while the Hyper-V parent partition implements a PowerShell Direct component that runs under the VMMS.exe process. PowerShell Direct uses VMBUS communication channel to communicate with the VMs running on the local Hyper-V host.

There are two ways to run PowerShell commands using the PowerShell Direct feature. For the first method, you must begin by entering the following command:

Enter-PSSession –VMName <VMName>

This command allows you to create a PowerShell interactive session against the VM name specified in the –VMName parameter. After running this command, you can use the Get-Service command or run any other PowerShell command. When finished, run the Exit-PSSession command.

When you implement the above series of PowerShell commands on a Hyper-V host, the PowerShell cmdlet (Get-Service) is executed inside the VM and the output is shown in the PowerShell window of the Hyper-V host. The second method involves using an Invoke-Command cmdlet to allow you to run a PowerShell command or a script against the VM specified in the –VMName parameter. Here is an example of using the Invoke-Command cmdlet:

At present, the PowerShell Direct feature isn't supported on VMs running on the remote Hyper-V host; the VMs must be running locally. It is also worth mentioning that the PowerShell Direct feature only works for VMs running Windows 10 and later OSes.

Join the conversation

1 comment

Register

I agree to TechTarget’s Terms of Use, Privacy Policy, and the transfer of my information to the United States for processing to provide me with relevant information as described in our Privacy Policy.

Please check the box if you want to proceed.

I agree to my information being processed by TechTarget and its Partners to contact me via phone, email, or other means regarding information relevant to my professional interests. I may unsubscribe at any time.