PowerShell script to rename the VHD file and attach it to the VM

I had few quries on how to re-name of the VHD same as VM name. This is a PowerShell script which you can ask as an alternative on getting this thing done.

This power shell uses the Hyper-v Module. Please download the module from https://pshyperv.codeplex.com/releases/view/38769 . This has the download and a PDF file which explain on how and what all commands are included in the module and how to use them.

Ø  Now download the zip file and then download it to the host machine. Unzip and save it in a folder (Eg: C:\PS\Hyperv_install).

Ø  Now to create a PowerShell prompt which will open up with importing the Hyper-v module.

Ø  For the same please do the following

o    Copy shortcut to Windows PowerShell from Start Menu/Accessories/Windows PowerShell to the desktop.

o    Now right click this shortcut and click on properties. In the box Target add parameter to the PowerShell.exe, you will need complete command line like this:

     %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -noexit -command "import-module : C:\PS\Hyperv_install\HyperV.psd1 | out-null"

Ø  These are the PowerShell which you can use for renaming

=====================================================================

#Written By Sudheesh N. This will take a input as the VM name and change the VHD name to the VMName.VHD
#Copy this code and create a file name RenameVHD.ps1

param ($VMname)
#Checking Root MS parameter
if ($VMname -eq $null) {
Write-Host "Virtual Disk name Not specified";
Write-Host "Syntax:";
Write-Host " RenameVHD.ps1 -VMname: <Name of the VM >";
exit;
}
[string]$VMname1 =$VMname
#Get the Disk path of the VM
$e = Get-VMDisk $VMName1| foreach {$_.DiskPath}
#Only Select the Disk IDE 0
[string]$m =%{$e[0]}
[string]$P=$m
$d = $m.ToCharArray()
for ($i=0 ; $i -le $m.length-1; $i++){if ($d[$i] -eq "\")
{$l=$i}}
[string]$x=""
for ($i=0 ; $i -le $l; $i++){$x=$x+$d[$i]}
$NewName=$VMName1 + ".vhd"
$P=($P.trim()).trimend("")
#Rename the VHD
Rename-Item -path $P -newname $NewName
$x=$x+$Newname
#Change the VM properties for the hard drive at IDE0 Channel 0
Set-VMDisk $VMName 0 0 $x
exit

======================================================================

Ø  In case you have lot of machines deployed to the host then you can automate this by using this script

=========================================================================

#Written By Sudheesh N. This will check which are machines are in stopped mode and then will rename the VHD of
#those to VM name by calling the previous script. Save this in the same folder of the previous script and name it as
#Name it as RenameVHDHost.ps1

#get the list of VM in host
$c=Get-VMSummary | foreach {$_.VMElementName}
#Check which of these are in stopped and then call the renamevm.ps1 for each VM which is in stopped state
foreach ($i in $c)
{
$s=Get-VMState $i | foreach {$_.Enabledstate}
if ($s -eq "Stopped")
{
.\ RenameVHD.ps1 -VMName:$i
}
}

========================================================================

Note: Please test these scripts before putting to some production.

Hope this is helpful

Sudheesh Narayanaswamy | Support Engineer | Microsoft