troubleshooting Question

Renaming Hyper-V VHD and VHDX with PowerShell - problems with syntax

Avatar of JWeb Admin
JWeb Admin asked on
PowershellHyper-VWindows Server 2012
14 Comments1 Solution4408 ViewsLast Modified:
I am trying to find a way to rename VHDs and VHDX's according to the Hyper-V VM name.  This script allows you to enter the name of the VM, and it will rename the disks to match the VM name.  

I'm having some issues with the syntax.  Eventually this needs to rename all VHDs and VHDX's if their names do not match.

#Enter Name for new VM
$VM = "VM1_Replica"
#Do not make any changes below this line
$vhd = Get-SCVirtualDiskDrive -VM $vm
Foreach ($vhd in $vhd)
    $oldName = $vhd.Path
    $newName = $oldName.Substring(0,$oldName.LastIndexOf("\")) `
                + "\" + $vhd.VMName + "_" + $vhd.ControllerType `
                + "_" + $vhd.ControllerNumber + "_" `
                + $vhd.ControllerLocation`
                + $oldName.Substring($oldName.LastIndexOf("."))
    Rename-Item $oldName $newName
    Set-SCVirtualDiskDrive -VMName $vhd.VMName -Path $newName `
        -ControllerType $vhd.ControllerType -ControllerNumber `
        $vhd.ControllerNumber -ControllerLocation `

I'm getting errors such as the ones below.  This may be related to VMM 2012 R2 PowerShell syntax being incorrect - this script may have been for an older version of VMM.

The environment this needs to work in is Server 2012 R2 and VMM 2012 R2.

You cannot call a method on a null-valued expression.
At line:11 char:5
+     $newName = $oldName.Substring(0,$oldName.LastIndexOf("\")) `
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
Rename-Item : Cannot bind argument to parameter 'Path' because it is null.
At line:16 char:17
+     Rename-Item $oldName $newName
+                 ~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Rename-Item], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.RenameItemCommand
Set-SCVirtualDiskDrive : A parameter cannot be found that matches parameter name 'VMName'.
At line:17 char:28
+     Set-SCVirtualDiskDrive -VMName $vhd.VMName -Path $newName `
+                            ~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Set-SCVirtualDiskDrive], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.SystemCenter.VirtualMachineManager.Cmdlets.SetVirtualDiskDriveCmdlet
Subash Sundharan
IT Infrastructure Architect

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 14 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 14 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros