Posted on January 19, 2017 · Posted in Hyper-V, Powershell, Windows Server 2016

Hyper-V PowerShell Direct in Windows Server 2016

PowerShell Direct is a new PowerShell feature in Windows Server 2016 and Windows 10 that allows to create a direct local PowerShell session with any virtual machine run on a Hyper-V host, meanwhile the connection is established not via the network, but  through the internal VM bus. Thus, a Hyper-V administrator can manage any VM using PowerShell even if it located in the isolated network or this VM without vNIC adapters.

The main requirements to use Powershell Direct:

  1. Windows Server 2016 or Windows 10 can be used as Hyper-V host OS
  2. A guest VM has to be run on the same Hyper-V host that provides connection (i.e. local connection)
  3. The guest VM OS has to be Windows Server 2016 / Windows 10 as well
  4. PowerShell console on the Hyper-V host has to be run with administrator privileges
  5. To connect to the guest OS, you need to specify username and password of the account in this VM

Earlier, an administrator could run commands in a Hyper-V virtual machine only over the network using Invoke-Command or Enter-PSSession. To connect to a remote VM, you have to specify its network name (-ComputerName).

For example, you can get the list of the processes running on a remote server like this:
Invoke-Command -ComputerName WinSrv16-Core -ScriptBlock {Get-Process}
Invoke-Command -ComputerName

Or create a new interactive PS session with the remote server:
Enter-PSSession -ComputerName WinSrv16-Core

Enter-PSSession -ComputerName WinSrv16-Core

In this scenario, a PS session cannot be established if the VM is not connected to the virtual switch, runs in an isolated or firewall-protected network. You can control such VM only using Hyper-V graphic console.

vm isolated from network on hyper v 2016

To connect to this guest VM using Powershell Direct, you don’t need a network name of the guest OS, but the VM name or its VMGUID in Hyper-V environment. When connecting using Powershell Direct, you can also use Enter-PSSession to create an interactive PowerShell session, or Invoke-Command to run a single command or a script.

  • Get the list of VMs on the Hyper-V host: Get-VM | Select Name
  • Start an interactive PowerShell session: Enter-PSSession -VMName "win10 Comp PS-Direct" -Credential (Get-Credential)
  • To exit the session, run: Exit-PSSession

Managing Hyper-V 2016 VMs using PowerShell Direct

To run a single command or script, use Invoke-Command:

Invoke-Command -VMId <VMId> -FilePath C:\ps\script.ps1

For example, if you need to copy a file to an isolated VM using Powershell Direct, you can use the following commands:

Create a new session PSSession1:

$PSSession1 = New-PSSession -VMId <VMId>  -Credential (Get-Credential)

Copy a file to the virtual machine:

Copy-Item -ToSession $PSSession1 -Path C:\win10_ent_x64.iso  -Destination  E:\ISO\

Copy a file from the VM:

Copy-Item -FromSession $PSSession1 -Path C:\vm_file.txt -Destination C:\hyperv_host_share\

As you can see, it is quite convenient and easy to use Powershell Direct. Like most other new features in Windows Server 2016, Powershell Direct, first of all, is focused on the improving the system functionality from the point of view of service/cloud providers providing virtual machine service.

Related Articles