Using PowerShell Direct for Hyper-V VMs

I have multiple Hyper-V Vms on my laptop for testing purposes. Sometimes, you need to update those or check things, and you can do this using PowerShell Direct. In this blog post, I will show you how to use PowerShell Direct with a few examples.


There are a few requirements for using PowerShell Direct, and these should not be an issue if you have a somewhat up-to-date environment 🙂 Requirements are:

HostWindows 10, Windows Server 2016, or later running Hyper-V
Guest/VMWindows 10, Windows Server 2016, or later

Using PowerShell Direct

Starting a Remote Session to a VM

You can use the Enter-PSSession cmdlet like when connecting a normal Windows Client or Server. But with PowerShell Direct, you can use the Parameter -VMName to specify the VM running on your system and log in directly from your Hyper-V Host, even when the VM has no network access.

In the example below, I’m connecting to my “Windows Server 2022 Member” VM, which is part of my test.local Active Directory. To demonstrate that you even can connect without the machine having an active Network Connection, I disabled the Network Adapter in the VM.

Note: You will still need to provide the Credentials for the VM. In my case, I used the local Administrator account.

You can see you’re in a Remote Session on the VM now because the prompt changed to [Windows Server 2022 Member]. You can run your command directory in the VM directly. I ran the ipconfig and ping commands to show that the VM has no Network Connection.

Running a script or command on a VM

It’s also possible to run a script or command without using Enter-PSSession. This is done by using the Invoke-Command cmdlet. In the example below, I run the Get-Service cmdlet on my member server and query all services with the status Running:

The syntax is almost the same to run a script on a VM. In the example below, I start the D:\Temp\Set_DNS_Primary_Secondary_All_Adapters.ps1 script, which reconfigures all network interfaces to use a different DNS server.

Running a script or command on multiple VMs

You can do this for multiple VMs. In the example below, I ran the same DNS change script on all the VMs on my laptop with ‘Windows Server 2022’ in their name. You have to set a global $Credential for this to work, which will be used to connect to the VMs. In the Foreach loop, the list of VMs is retrieved and used to run the Invoke-Command cmdlet. The script will show the results, and one of the two VMs already had the correct setting because I ran it as an example in the chapter before this one.

Copy Files and Folders to a VM

Using the Copy-Item cmdlet, you can also use PowerShell Direct to copy files or folders to a VM. In the example below, I copied a folder from my local D:\ drive to my laptop’s Windows Server 2022 Member VM.

First, you will have to create a new PSSession to the VM:

When the $Session is created, you can use that to transport a folder or file using Copy-Item: (Use the -Recurse Parameter to copy all files and subdirectories, otherwise it will just create an empty c:\temp\admx folder)

And the files are copied to the VM, as you can see in the screenshot below:

Copy Files and Folders to multiple VMs

Instead of just copying files or folders to one VM, you can also copy it to multiple VMs if they can be accessed using the same credentials (All workgroup VMs with the same Administrator account or Domain-Joined VMs). In the example script below, I copy the same D:\Temp\admx folder to two VMs (A Domain controller and a member server). I first retrieve a global $Credential set which I reuse on the VMs, then I create a PSSession with the VM in the Foreach loop containing all VMs with ‘Windows Server 2022’ in their name and copy the D:\Temp\admx folder to that VM)

Use cases

I use this to copy files or update things on multiple VMs when running VMs in an exam prep lab or testing stuff for customers. You can use this to automate the deployment of your VMs, too, I might do a blog post about that somewhere in the future 😉

More information

You can find more information about PowerShell Direct here.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.