How to Create / Remove a VHD File in Windows 7 with PowerShell
With this PowerShell script we're able to install a VHD file in Windows 7:
Copy this script below into a notepad and name it to setup.ps1
#Set the colors for the console window
$Host.Ui.RawUi.BackGroundColor = "Black"
$Host.Ui.RawUi.ForeGroundColor = "White"
#Remove old private settings
if(test-path C:\Windows\VHD\Private.txt) {remove-item C:\Windows\VHD -recurse;
Write-Host -ForegroundColor white -BackgroundColor red "Old File Removed"
}
#Clear the screen
cls
Function WriteHeading ($HeadingText)
{
Write-Host
Write-host "[" -foregroundcolor "DarkGray" -NoNewLine
Write-host "[" -foregroundcolor "Gray" -NoNewLine
Write-host "[" -foregroundcolor "White" -NoNewLine
Write-host $HeadingText -foregroundcolor "green" -NoNewLine
Write-host "]" -foregroundcolor "White" -NoNewLine
Write-host "]" -foregroundcolor "Gray" -NoNewLine
Write-host "]" -foregroundcolor "DarkGray"
Write-Host
}
Function WriteValue ($Label, $Value)
{
Write-Host $Label -foregroundcolor "Gray" -NoNewLine
Write-Host ": " -foregroundcolor "Gray" -NoNewLine
Write-Host $Value
}
Function WriteSuccess
{
Write-Host ": " -foregroundcolor "Gray" -NoNewLine
Write-Host Success -foregroundcolor "Green"
}
Function Get-Settings() {
WriteHeading " Loading Settings.... "
[xml]$settings = Get-Content C:\install\private\settings.xml
return $settings.settings
}
trap
{
Write-Error $_.Exception
break;
}
[System.Xml.XmlElement]$settings = Get-Settings
if ($settings -eq $null) {
return $false
}
$BootEntry = $settings.BootEntry
$Drive = $BootEntry.Drive
WriteValue "Drive" $Drive
$Path = $BootEntry.Path
WriteValue "Path" $Path
$Description = $BootEntry.Description
WriteValue "Description" $Description
WriteHeading " Copying Current Boot Entry "
$BootEntryCopy = bcdedit /copy '{current}' /d $BootEntry.Description
WriteSuccess
WriteHeading " Parsing New Boot Entry GUID "
$CLSID = $BootEntryCopy | ForEach-Object {$_.Remove(0,37).Replace(".","")}
WriteSuccess
Write-Host
WriteValue "New Boot Entry GUID" $CLSID
WriteHeading " Creating New Boot Entry "
$VHD = 'vhd=[' + $Drive + ':]\' + $Path
bcdedit /set $CLSID device $VHD
bcdedit /set $CLSID osdevice $VHD
bcdedit /set $CLSID detecthal on
Write-Host
WriteSuccess
WriteHeading " Change boot entry "
bcdedit /set description " First Partition "
Write-Host
WriteSuccess
Write-Host
#Create Directory
new-item c:\Windows\VHD -itemtype directory
new-item c:\Windows\VHD\Private.txt -type file
Write-Host
WriteSuccess
Write-Host
#Write into file
$file = "C:\Windows\VHD\Private.txt"
$CLSID | add-Content -Path $file
Write-Host
WriteSuccess
Write-Host
<==
Copy this into settings.xml
<?xml version="1.0" encoding="utf-8"?>
<Settings>
<BootEntry Drive="C" Path="VHD\Private.VHD" Description="Second Partition" />
</Settings>
Copy this into disk
select vdisk file=c:\VHD\Private.vhd
attach vdisk
assign letter=E
exit
Execute the following command in a cmd.exe with admin rights:
Powershell Get-ExecutionPolicy
Powershell Set-ExecutionPolicy RemoteSigned
Powershell C:\Install\Private\Setup.ps1
Now your VHD file is created into the boot options
If you need to remove the VHD file you can execute the following powershell script
Copy the script below into a notepad and save it to remove.ps1
#Set the colors for the console window
$Host.Ui.RawUi.BackGroundColor = "Black"
$Host.Ui.RawUi.ForeGroundColor = "White"
#Clear the screen
cls
Function WriteHeading ($HeadingText)
{
Write-Host
Write-host "[" -foregroundcolor "DarkGray" -NoNewLine
Write-host "[" -foregroundcolor "Gray" -NoNewLine
Write-host "[" -foregroundcolor "White" -NoNewLine
Write-host $HeadingText -foregroundcolor "green" -NoNewLine
Write-host "]" -foregroundcolor "White" -NoNewLine
Write-host "]" -foregroundcolor "Gray" -NoNewLine
Write-host "]" -foregroundcolor "DarkGray"
Write-Host
}
Function WriteValue ($Label, $Value)
{
Write-Host $Label -foregroundcolor "Gray" -NoNewLine
Write-Host ": " -foregroundcolor "Gray" -NoNewLine
Write-Host $Value
}
Function WriteSuccess
{
Write-Host ": " -foregroundcolor "Gray" -NoNewLine
Write-Host Success -foregroundcolor "Green"
}
# Read GUID from C:\Windows\VHD\Private.txt
$CLISD = Get-Content C:\Windows\VHD\Private.txt
WriteValue "GUID" $CLISD
WriteSuccess
#Remove BootEntry in Bootmenu
WriteHeading "Removing BootEntry in BootMenu"
BCDEDIT /delete $CLISD
Write-Host
Writesuccess
Write-Host
#Remove old private settings
if(test-path C:\Windows\VHD\Private.txt) {remove-item C:\Windows\VHD -recurse;
Write-Host -ForegroundColor white -BackgroundColor red "Old File Removed"
}
#Remove old private settings
if(test-path C:\VHD\private.VHD) {remove-item C:\VHD -recurse;
Write-Host -ForegroundColor white -BackgroundColor red "Old File Removed"
}
#Restart the Local host
Restart-Computer