Performing daily task with PowerShell to manage Hyper-V Server

As a virtualization administrator, you will come across a lot of scenarios where you will need to create, modify, move, export, and other tasks to manage your virtual machines every day. In some examples, you will need to change a few small and easy settings, which can be done via a graphical interface. However, you will also get cases where lots of virtual machines will need some advanced configuration or some settings that take a long time to complete.

It’s a fact that PowerShell is a handy and strong ally in all these examples, and this recipe will show some examples of how to perform daily tasks such as disk, network, memory, export, and virtual machine manipulation using a couple of small and simple PowerShell commandlets.

You can even perform this tasks on several Hyper-V Servers remotely without the need of login on the servers. To know how to do it, visit my previous post Managing Hyper-V Server remotely through PowerShell

NOTE: Make sure that you have a PowerShell window opened as administrator before you start.

How to do it

These tasks show lots of handy examples of daily tasks that can be used to help you administer your Hyper-V servers, such as creating and changing VHDs, virtual switches, VM tasks, migrations, and much more.

1. Let’s start with a simple command New-VHD, to create a virtual hard disk for a VM. Type the following command to create a 20GB VHDX file named NewDisk on the H: partition.

New-VHD -SizeBytes 20GB –Path H:NewDisk.vhdx

2. To add the created VHDX file to a VM, use the command Add-VMHardDiskDrive, as shown here:

Add-VMHardDiskDrive -VMName NewVM -Path H:Hyper-VNewDisk.vhdx

3. To create a new virtual switch, you can use the New-VMSwitch command. The following example creates an external switch and binds it to a network adapter called Ethernet Connection.

New-VMSwitch “External Switch” –NetAdapterName “Ethernet Connection” –AllowManagementOS $true

4. To add a network adapter to a VM, use the Add-VMNetworkAdapter command. The next command adds a network adapter named Prod NIC to all the virtual machines that start with Prod.

Add-VMNetworkAdapter -VMName Prod* -Name “Prod NIC”

5. Use the Connect-VMNetworkAdapter to add VMs to a virtual switch. The following command gets all the VMs starting with TestVM and adds to a switch called Private Switch.

Connect-VMNetworkAdapter -VMName TestVM* -SwitchName ‘Private Switch’

6. To add a legacy network adapter to a virtual machine, you can also use the Add-VMNetworkAdapter with the IsLegacy switch. The following example shows the usage of this command. This gets all the VMs starting with NewVM and adds a legacy network named BootableNIC.

Get-VM NewVM* | Add-VMNetworkAdapter -IsLegacy $true –Name BootableNIC

7. You can use the Set-VMNetworkAdapter to change the virtual machine network adapter settings. The first command below is changing the maximum and minimum bandwidth configuration to all virtual machine starting with VMTest. The second command enables Mac address spoofing to all VMs that end with NLB.

Set-VMNetworkAdapter -VMName VMTest* -MaximumBandwidth 100MB -MinimumBandwidthAbsolute 20MB

Set-VMNetworkAdapter -VMName *NLB -MacAddressSpoofing On

8. To add Fibre Channel HBAs to virtual machines you can use the Add-VMFibreChannelHBA as the next example:

Add-VMFibreChannelHba -VMName NewVM -SanName VMProd

9. You can also use basic tasks such as starting and stopping virtual machines using the Start-VM and Stop-VM commands, as shown in the following two examples:

Start-VM -Name SPVM*

Stop-VM -Name TestVM -TurnOff

10. To create virtual machine snapshots you can use the tricky command Checkpoint-VM. The next example creates a snapshot called PreMigrationSnapshot to all the VMs starting with ProdServer.

Checkpoint-VM -Name ProdServer* -SnapshotName PreMigrationSnapshot

11. To create a virtual machine from a snapshot you can use the Export-VMSnapshot command as follows:

Export-VMSnapshot -Name ‘PosUpdates’ -VMName NewVM -Path H:NewVMfromSnapshot

12. In the case of a server migration, you can use the Export-VM command to export VMs to a local folder. The following command shows a handy example of all the virtual machines being exported to a local drive in their own folders.

Get-VM | Export-VM -Path H:ExportedVMs

13. To move the virtual machine storage, use the Move-VMStorage command, specifying the destination path that you want to move the VM storage to, as shown in the following example:

Move-VMStorage NewVM -DestinationStoragePath H:NewVM

14. For moving all the storage from local VMs to a new storage, by creating a folder for each migrated VM, you can use the next example:

Get-VM | %{ Move-VMStorage $_.Name “H:Hyper-V$($_.Name)” }

15. Use the Set-VM to change the VM settings. In the next example, all servers starting with VMSharePoint are having their dynamic memory enabled with the minimum, maximum, and startup values being configured. There’s also a command which only changes the memory settings, called Set-VMMemory. The second example does exactly the same thing as the first, just using different commands.

Set-VM -Name VMSharePoint* -DynamicMemory -MemoryMinimumBytes 8GB -MemoryMaximumBytes 12GB -MemoryStartupBytes 10GB

Set-VMMemory -VMName VMSharePoint* -DynamicMemoryEnabled $true -MaximumBytes 12GB -MinimumBytes 8GB -StartupBytes 10GB



From simple tasks, such as starting a VM, to advanced ones, such as moving all virtual machine storage to a new location, it is much easier to use PowerShell rather than the GUI interface. From the 164 Hyper-V commandlets, you have seen examples of the following type (you can find all commandlets here):

  • Add-VMFibreChannelHba
  • Add-VMHardDiskDrive
  • Add-VMNetworkAdapter
  • New-VMSwitch
  • Connect-VMNetworkAdapter
  • New-VHD
  • Checkpoint-VM
  • Export-VMSnapshot
  • Move-VMStorage
  • Set-VM
  • Set-VMMemory
  • Set-VMNetworkAdapter
  • Start-VM
  • Stop-VM

These are the normal commands used day-to-day in order to create disks and networks, change VM settings, start VMs, add fibre channels adapters, create snapshots, migrate VMs, and other tasks that can be easily done via PowerShell.

You might encounter other tasks that will require different commands, but with this start, you can have an idea of commands and the things you can do via PowerShell.

Tips and Tricks

Switch Whatif

If you are not sure whether a commandlet will work or what the result will be, you can test it before you run it. The new switch Whatif added at the end of the command PowerShell can tell you whether it’s going to work or not.

The following screenshot shows a command that uses the whatif option and when executed, PowerShell explains that it will not work and why. After fixing it, you can try using the whatif command again. For the Export-VM command, you will see the What if: Export-VM will export the virtual machine “NewVM1” message.


Using PowerShell ISE for advanced script editing

For advanced and big scripts, you can use a very interesting tool named PowerShell ISE.

It offers a GUI PowerShell window with colors, line count, command predict, error verification, and a debugging option, making your scripting experience easier and faster.

The next screenshot shows an example of a script being written by PowerShell ISE with a window showing the command prediction feature, and the command column in the pane on the right-hand side.


Marcos Nogueira

With more than 18 years experience in Datacenter Architectures, Marcos Nogueira is currently working as a Principal Cloud Solution Architect. He is an expert in Private and Hybrid Cloud, with a focus on Microsoft Azure, Virtualization and System Center. He has worked in several industries, including Aerospace, Transportation, Energy, Manufacturing, Financial Services, Government, Health Care, Telecoms, IT Services, and Gas & Oil in different countries and continents. Marcos was a Canadian MVP in System Center Cloud & Datacenter Managenment and he has +14 years as Microsoft Certified, with more than 100+ certifications (MCT, MCSE, and MCITP, among others). Marcos is also certified in VMware, CompTIA and ITIL v3. He assisted Microsoft in the development of workshops and special events on Private & Hybrid Cloud, Azure, System Center, Windows Server, Hyper-V and as a speaker at several Microsoft TechEd/Ignite and communities events around the world.

Leave a Reply

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