I need to automate the process of exporting a virtual machine recently – and when I checked around I found that while there are a number of sample scripts out there that show you how to do this (some even on this blog) they are all written in VBScript. As I am now to the stage where PowerShell is my preferred scripting language – I sat down and wrote up this short script:
Some quick notes to make about this script:
- This script takes a Hyper-V server name, a virtual machine name and an export path – and then performs a full export to the requested location.
- I am using the older (deprecated) ExportVirtualSystem method here – and not ExportVirtualSystemEx (which I really should do). The main reason for this is that while ExportVirtualSystemEx is a lot more powerful than ExportVirtualSystem, ExportVirtualSystem works perfectly fine for this basic use case and is much easier to script.