title | description | ms.topic | ms.assetid | ms.author | author | ms.date |
---|---|---|---|---|---|---|
Manage Windows virtual machines with PowerShell Direct |
Gives instructions for using PowerShell Direct to manage virtual machines without relying on a network or remote connection to them. |
article |
b5715c02-a90f-4de9-a71e-0fc09093ba2d |
roharwoo |
robinharwood |
10/04/2016 |
You can use PowerShell Direct to remotely manage a Windows 10, Windows Server 2016, or Windows Server 2019 virtual machine from a Windows 10, Windows Server 2016, or Windows Server 2019 Hyper-V host. PowerShell Direct allows Windows PowerShell management inside a virtual machine regardless of the network configuration or remote management settings on either the Hyper-V host or the virtual machine. This makes it easier for Hyper-V Administrators to automate and script virtual machine management and configuration.
There are two ways to run PowerShell Direct:
-
Create and exit a PowerShell Direct session using PSSession cmdlets
-
Run script or command with the Invoke-Command cmdlet
If you're managing older virtual machines, use Virtual Machine Connection (VMConnect) or configure a virtual network for the virtual machine.
-
On the Hyper-V host, open Windows PowerShell as Administrator.
-
Use the Enter-PSSession cmdlet to connect to the virtual machine. Run one of the following commands to create a session by using the virtual machine name or GUID:
Enter-PSSession -VMName <VMName>
Enter-PSSession -VMId <VM GUID>
-
Type your credentials for the virtual machine.
-
Run whatever commands you need to. These commands run on the virtual machine that you created the session with.
-
When you're done, use the Exit-PSSession to close the session.
Exit-PSSession
You can use the Invoke-Command cmdlet to run a pre-determined set of commands on the virtual machine. Here is an example of how you can use the Invoke-Command cmdlet where PSTest is the virtual machine name and the script to run (foo.ps1) is in the script folder on the C:/ drive:
Invoke-Command -VMName PSTest -FilePath C:\script\foo.ps1
To run a single command, use the -ScriptBlock parameter:
Invoke-Command -VMName PSTest -ScriptBlock { cmdlet }
To create a PowerShell Direct session on a virtual machine,
-
The virtual machine must be running locally on the host and booted.
-
You must be logged into the host computer as a Hyper-V administrator.
-
You must supply valid user credentials for the virtual machine.
-
The host operating system must run at least Windows 10 or Windows Server 2016.
-
The virtual machine must run at least Windows 10 or Windows Server 2016.
You can use the Get-VM cmdlet to check that the credentials you're using have the Hyper-V administrator role and to get a list of the virtual machines running locally on the host and booted.