
Download Windows Server 2012
Download Your FREE Copy of Hyper-V Server 2012
This PowerShell script illustrates how to batch rename multiple virtual machines by using PowerShell 3.0 in Windows Server 2012.
Batch rename virtual machines is a frequently asked question in many public forums. IT Admin wants to batch rename virtual machines like add prefix name or postfix name or replace specific string with multiple virtual machines name. Normally, we need to change the name one by one, it is really time-consuming.
Step1: Run the script in the Windows PowerShell Console, type the one command: Import-Module <Script Path> at the prompt.
For example, type Import-Module C:\Script\ BatchRenameVirtualMachines.psm1
The step is shown in the following figure.

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

Example 1: How to batch add prefix name with 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 Rename-OSCVM –IDScope 1..3 -PrefixName “NYK-” command will add postfix name with your specified virtual machine scope.

The result is shown in the following figure, these virtual machines name has been modified as we expected.

Example 2: How to batch replace specific string with 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 Rename-OSCVM –IDScope 2,3 -Pattern “NYK-” –NewPattern “BJ-” command will add postfix name with your specified virtual machine scope.

The result is shown in the following figure, these virtual machines name has been modified as we expected.

Here are some code snippets for your references.
If($PrefixName)
{
If ($ExcludeIDScope)
{
$ModifyVMs = Find-OSCVirtualMachine -ExcludeIDScope $ExcludeIDScope
Foreach($ModifyVM in $ModifyVMs)
{
$ModifyVMName = $ModifyVM.VirtualMachineName
Rename-VM -Name $ModifyVMName -NewName "$PrefixName$ModifyVMName"
Write-Host "Renamed the virtual machine ""$ModifyVMName"" to ""$PrefixName$ModifyVMName""." -ForegroundColor Green
}
}
Else
{
$ModifyVMs = Get-OSCVirtualMachine -IDScope $IDScope
Foreach($ModifyVM in $ModifyVMs)
{
$ModifyVMName = $ModifyVM.VirtualMachineName
Rename-VM -Name $ModifyVMName -NewName "$PrefixName$ModifyVMName"
Write-Host "Renamed the virtual machine ""$ModifyVMName"" to ""$PrefixName$ModifyVMName""." -ForegroundColor Green
}
}
}
If($PrefixName) { If ($ExcludeIDScope) { $ModifyVMs = Find-OSCVirtualMachine -ExcludeIDScope $ExcludeIDScopeForeach($ModifyVMin$ModifyVMs) { $ModifyVMName = $ModifyVM.VirtualMachineName Rename-VM -Name $ModifyVMName-NewName "$PrefixName$ModifyVMName" Write-Host "Renamed the virtual machine ""$ModifyVMName"" to ""$PrefixName$ModifyVMName""."-ForegroundColor Green } } Else { $ModifyVMs = Get-OSCVirtualMachine -IDScope $IDScopeForeach($ModifyVMin$ModifyVMs) { $ModifyVMName = $ModifyVM.VirtualMachineName Rename-VM -Name $ModifyVMName-NewName "$PrefixName$ModifyVMName" Write-Host "Renamed the virtual machine ""$ModifyVMName"" to ""$PrefixName$ModifyVMName""."-ForegroundColor Green } } }
Windows PowerShell 3.0
Windows Server 2012