How to batch export virtual machines in Windows Server 2012 (PowerShell)

Introduction

This PowerShell Script shows how to batch export multiple virtual machines by using PowerShell 3.0 in Windows Server 2012.

Download Windows Server 2012
Download Your FREE Copy of Hyper-V Server 2012

Scenarios

Assume that an IT administrator wants to batch multiple export virtual machines with one command line. Usually, you have to change the name one by one.

Script

Step1: Run the script in the Windows PowerShell Console, and then type the one command: Import-Module <Script Path> at the prompt.
For example, type Import-Module C:\Script\ BatchExportVirtualMachines.psm1
This is shown in the following figure.

Step 2: Type the command Get-Help Export-OSCVM -Full
to display the entire help file for this function, such as the syntax, parameters, or examples.

Here are some code snippets for your references. To get the complete script sample, please click the download button at the beginning of this page.

PowerShell
Edit|Remove
If($ExcludeID-match "..") 
{ 
    Foreach($ExcludeIDinInvoke-Expression([String]$ExcludeIDScope)) 
    { 
        #Perform the recursion by calling itself.$ModifyVirtualMachines = $ModifyVirtualMachines| Where-Object ID -NE $ExcludeID 
    } 
} 
Else 
{ 
    Foreach($ExcludeIDin$ExcludeIDScope) 
    { 
        #Perform the recursion by calling itself.$ModifyVirtualMachines = $ModifyVirtualMachines| Where-Object ID -NE $ExcludeID 
    } 
}

Example

Example 1: How to batch export specified virtual machines
First, we need to type Show-OSCVirtualMachineInfo command to obtain currently virtual machine information in the Windows PowerShell Console.

After that, type Export-OSCVM -IDScope 1..5 -Path D:\VM command shows how to batch export virtual machines to the D:\VM.

The result is shown below:

Prerequisite

Windows PowerShell 3.0
Windows Server 2012

Additional Resources

New-Object