| title | Working with Hyper-V and Windows PowerShell |
|---|---|
| description | Working with Hyper-V and Windows PowerShell |
| keywords | windows 10, hyper-v |
| author | scooley |
| ms.author | scooley |
| ms.date | 05/02/2016 |
| ms.topic | article |
| ms.prod | windows-10-hyperv |
| ms.assetid | 6d1ae036-0841-4ba5-b7e0-733aad31e9a7 |
Now that you have walked through the basics of deploying Hyper-V, creating virtual machines and managing these virtual machines, let’s explore how you can automate many of these activities with PowerShell.
- Click on the Windows start button, type PowerShell.
- Run the following command to display a searchable list of PowerShell commands available with the Hyper-V PowerShell Module.
Get-Command -Module hyper-v | Out-GridViewYou get something like this:
- To learn more about a particular PowerShell command use
Get-Help. For instance running the following command returns information about theGet-VMHyper-V command.
Get-Help Get-VMThe output shows you how to structure the command, what the required and optional parameters are, and the aliases that you can use.
Use the Get-VM command to return a list of virtual machines.
- In PowerShell, run the following command:
Get-VMThis displays something like this:
- To return a list of only powered on virtual machines add a filter to the
Get-VMcommand. A filter can be added by using theWhere-Objectcommand. For more information on filtering see the Using the Where-Object documentation.
Get-VM | where {$_.State -eq 'Running'}- To list all virtual machines in a powered off state, run the following command. This command is a copy of the command from step 2 with the filter changed from 'Running' to 'Off'.
Get-VM | where {$_.State -eq 'Off'}- To start a particular virtual machine, run the following command with name of the virtual machine:
Start-VM -Name <virtual machine name>- To start all currently powered off virtual machines, get a list of those machines and pipe the list to the
Start-VMcommand:
Get-VM | where {$_.State -eq 'Off'} | Start-VM- To shut down all running virtual machines, run this:
Get-VM | where {$_.State -eq 'Running'} | Stop-VMTo create a checkpoint using PowerShell, select the virtual machine using the Get-VM command and pipe this to the Checkpoint-VM command. Finally give the checkpoint a name using -SnapshotName. The complete command looks like the following:
Get-VM -Name <VM Name> | Checkpoint-VM -SnapshotName <name for snapshot>The following example shows how to create a new virtual machine in the PowerShell Integrated Scripting Environment (ISE). This is a simple example and could be expanded on to include additional PowerShell features and more advanced VM deployments.
- To open the PowerShell ISE click on start, type PowerShell ISE.
- Run the following code to create a virtual machine. See the New-VM documentation for detailed information on the
New-VMcommand.
$VMName = "VMNAME"
$VM = @{
Name = $VMName
MemoryStartupBytes = 2147483648
Generation = 2
NewVHDPath = "C:\Virtual Machines\$VMName\$VMName.vhdx"
NewVHDSizeBytes = 53687091200
BootDevice = "VHD"
Path = "C:\Virtual Machines\$VMName"
SwitchName = (Get-VMSwitch).Name
}
New-VM @VMThis document has shown some simple steps to explorer the Hyper-V PowerShell module as well as some sample scenarios. For more information on the Hyper-V PowerShell module, see the Hyper-V Cmdlets in Windows PowerShell reference.


