Export Hyper-V Virtual Machines via script

/ / Tech Blog

Here is a powershell script to export all your running VM’s from Hyper v

This script starts by getting a list of all the VMs that are currently not turn off and storing them in a verbal called $RunningVms

Then it will cycle thought all the VMs one by one turning them off, Exporting them to the location that you have asked and then turning them back on, it will then wait 60 seconds and move to the next VM

all you need to change on this is two verbals one to tell the script if you are using a hyper v cluster and the other to tell it where you would like to store the exported VMs


#------------------------------------------------------------------------------------------#
# Title: Export VMs from Hyper V
# Compainey: ITSSIE
# Description: This will get a list of all the VM's that are Currenty Running on hyper v and then
# turn them off and export them to a backup location then turn them back on,
# it will do this one by one for each VM on your hyper v system
# Version: 1.0
# Script type: Power Shell Script
# Changed By:
# Changes Made:
# Change Date:
#------------------------------------------------------------------------------------------#

$UsingCluster = "No" # If you are using a Cluster of VM's then chnage this to Yesbut if it is just one Host then leave it as No
$BackupLocation = "C:\TempBackup\" # Change this to the location that you want the exported vm to be saved to

# Do not edit below this line

Remove-Item -Path $BackupLocation -Recurse
mkdir $BackupLocation

If ($usingCluster -Eq "No") {
$RunningVms = Get-VM | Where-Object {$_.State -ne "Off"}
$RunningVms has a list of all VMs that the script will export

} Else { $RunningVms = Get-ClusterNode; ForEach($item in $RunningVms) {Get-VM -ComputerName $item.Name; } } ForEach ($name in $RunningVms) { Stop-VM –Name $name.name -TurnOff -Force Eport-VM –Name $name.name –Path $BackupLocation Start-VM –Name $name.name Start-Sleep -s 60 } 

This script comes as is and has no support