This content is part of the Essential Guide: The go-to Windows PowerShell guide

How do I enable and use PowerShell Direct?

The Windows Server 2016 PowerShell Direct feature solves common management problems by eliminating the need for network connection or security policy modification when executing PowerShell commands.

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:

Invoke-Command –VMName TestVM –FilePath C:\VMScripts\Script1.PS1
Invoke-Command –VMName TestVM –ScriptBlock { Get-Process }

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.

Next Steps

Anticipation builds for Windows Server 2016 features

Ensuring a painless Windows Server 2016 upgrade

Why you should consider upgrading Windows Server

Dig Deeper on Microsoft Hyper-V management