Note: the following sections are taken from the Microsoft website:
Now that you have a basic understanding of how to deploy Hyper-V, create virtual machines, and manage these virtual machines, let's look at how to use PowerShell to automate most of these activities.
Returns a list of Hyper-V commands
- Click the Windows Start button, and type "PowerShell".
- powershell
-
get-command -module hyper-v | out-gridview
The contents of the fetch are as follows:
650) this.width=650; "Src=" https://docs.microsoft.com/zh-cn/ Virtualization/hyper-v-on-windows/quick-start/media/command_grid.png "style=" Border:0px;height:auto;/>
-
To learn more about specific PowerShell commands, use theGet-Help. For example, run the following command to returnGet-VMinformation about the Hyper-V command.
PowerShell
Get-help GET-VM
The output shows you how to build the command, the required and optional parameter definitions, and the aliases that you can use.
650) this.width=650; "Src=" https://docs.microsoft.com/zh-cn/virtualization/hyper-v-on-windows/quick-start/media/ Get_help.png "style=" Border:0px;height:auto; "/>
Return to the list of virtual machines
using GET-VM command returns the list of virtual machines.
- powershell
-
GET-VM
650) this.width=650; "Src=" Https://docs.microsoft.com/zh-cn/virtualization/hyper-v-on-windows/quick-start/media /get_vm.png "style=" Border:0px;height:auto; "/>
-
To return only the list of virtual machines that have been started, add a filter to theGet-VMcommand. You canWhere-Objectadd filters by using commands. For more information about filtering, see Working with where-object documents.
PowerShell
GET-VM | where {$_. State-eq ' Running '}
- To list all virtual machines that are in the shutdown state, run the following command. This command is a copy of the command in step 2, but the filter is changed from running to off.
-
PowerShell
GET-VM | where {$_. State-eq ' Off '}
To start and shut down a virtual machine
- To start a specific virtual machine, run the following command with the virtual machine name:
-
PowerShell
Start-vm-name <virtual Machine name>
-
powershell
get-vm | where {$_. state -eq ' Off '}&NBSP;|&NBSP;START-VM
- To close all running virtual machines, run the following command:
-
PowerShell
GET-VM | where {$_. State-eq ' Running '} | Stop-vm
To create a VM checkpoint
To create checkpoints using PowerShell, use theGet-VMcommand to select a virtual machine, and then pass the virtual machine through a pipe to theCheckpoint-VMCommand. Finally, use-SnapshotNameName the checkpoint. The complete command looks like this:
PowerShell
Get-vm-name <VM name> | Checkpoint-vm-snapshotname <name for Snapshot>
Create a new virtual machine
The following example shows how to create a new virtual machine in the PowerShell Integrated Scripting Environment (ISE). This is a simple example and can be extended to include additional PowerShell features and more advanced VM deployments.
- To open PowerShell Ise, click Start, and type PowerShell ise.
-
Run the following code to create the virtual machine. ForNew-VMmore information about commands, see the NEW-VM documentation.
PowerShell
$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[0]
}New-VM @VM
Summary and Reference
This document describes some simple steps for studying the Hyper-V PowerShell module and some example scenarios. For more information about the Hyper-V PowerShell module, see Hyper-V Cmdlets PowerShell command prompt http://hangyc.blog.51cto.com/2016 series tutorial 427521/1965057
This article is from "Hangzhou Technology blog" blog, please be sure to keep this source http://hangyc.blog.51cto.com/427521/1965724
Hyper-V 2016 series tutorial 39 using Hyper-V and Windows PowerShell in Windows 10