PowerShell Direct

Powershell direct is a feature in Server 2016 and upwards whereby we can use Powershell to administer Hyper-V virtual machines that are hosted on the host your managing from.

To administer VMs in this way, we use the Invoke-Command cmdlet to send commands to the machine, or we can create a remote Powershell session using the Enter-PSSession cmdlet with the use of the -VMName parameter.

#First, store credentials for use in the following commands
$cred = Get-Credential
#Invoke Command Method
Invoke-Command -VMName VM1 -Credential $cred -ScriptBlock {Get-NetIPAddress}
#Remote Session Method
Enter-PSSession -VMName VM1 -Credential $cred

The second method gives you a remote session so you can working with the VM interactively and see the host output.

Another feature of this is the ability to copy files from the host to the VM using Powershell.

#Create a session and copy a file from HV host to the VM.
$session = New-PSSession -VMName VM1 -Credential $cred
Copy-Item -ToSession $session -Path C:\myfile.txt -Destination C:\

You can also do this in reverse, copying a file from a VM to the host like this:

Copy-Item -FromSession $session -Path C:\myfile.txt -Destination C:\

Leave a Reply

Your email address will not be published. Required fields are marked *