CMD file to add a VHD(x) boot object in BCD

Page 1 of 2 12 LastLast

  1. Posts : 2,651
    Windows 7 Pro
       #1

    CMD file to add VHD(x) and WIM boot objects in BCD


    Hi,

    This CMD File can be used to easily add boot entries for VHD(x) and WIM images. With one command.

    abo.cmd:
    Code:
    @echo off
    if "%1"=="" Goto :help
    if "%1"=="ramdisk" goto ramdisk
    if "%1"=="vhd" goto vhd
    
    :ramdisk
    for /f "tokens=2 delims={}" %%g in ('bcdedit.exe /create /d %2 /Device') do (set dvid={%%g})
    bcdedit /set %dvid% ramdisksdidevice partition=%3
    bcdedit /set %dvid% ramdisksdipath %5
    for /f "tokens=2 delims={}" %%g in ('bcdedit.exe /create /d %2 /application osloader') do (set guid={%%g})
    bcdedit /set %guid% device ramdisk=[%3]%4,%dvid%
    bcdedit /set %guid% osdevice ramdisk=[%3]%4,%dvid%
    bcdedit /set %guid% systemroot \windows
    bcdedit /set %guid% path \Windows\system32\boot\winload.%6
    bcdedit /set %guid% winpe yes
    bcdedit /set %guid% detecthal yes
    bcdedit /displayorder %guid% /addlast
    bcdedit /set {bootmgr} displaybootmenu True
    bcdedit /set {bootmgr} timeout 5
    goto :Eof
    
    :vhd
    for /f "tokens=2 delims={}" %%g in ('bcdedit.exe /create /d %2 /Device') do (set dvid={%%g})
    bcdedit /set %dvid% device vhd=[%3]%4
    for /f "tokens=2 delims={}" %%g in ('bcdedit.exe /create /d %2 /application osloader') do (set guid={%%g})
    bcdedit /set %guid% device vhd=[%3]%4,%dvid%
    bcdedit /set %guid% osdevice vhd=[%3]%4,%dvid%
    bcdedit /set %guid% systemroot \windows
    bcdedit /set %guid% path \Windows\system32\boot\winload.%5
    bcdedit /set %guid% winpe no
    bcdedit /set %guid% detecthal yes
    bcdedit /displayorder %guid% /addlast
    bcdedit /set {bootmgr} displaybootmenu True
    bcdedit /set {bootmgr} timeout 5
    goto :Eof
    
    :help
    echo.
    echo Usage: abo ramdisk Name Drive: \Path\Boot.wim \Path\Boot.sdi efi/exe
    echo Usage: abo vhd Name Drive: \Path\Boot.vhdx efi/exe
    echo.

    Ex:
    Code:
    abo ramdisk Reflect8 D: \BootImages\Reflect8\Boot.wim \BootImages\Reflect8\Boot.sdi efi
    Will create a EFI Boot entry for Reflect8 Ramdisk

    Ex:
    Code:
    abo vhd Win10Pro D: \Win10Pro.vhdx exe
    Will create a MBR Boot entry for Windows10 pro VHD

    Limitations:
    Does not support "space" in Name.
    If there are spaces in paths, they must be "in quotes"

    Enjoy !!!
    Last edited by MaloK; 20 Apr 2022 at 14:32.
      My Computers


  2. Posts : 13,695
    Windows10
       #2

    MaloK said:
    Hi,

    I'm working on a cmd file to add Boot objects in one command, I would like it to support WIM and VHD.

    I'm not really familiar with the process of adding VHD boot entry. I and the ones I tried failed to boot, Can someone show how to properly add one ?

    This is what I got so far working with WIM images.

    abo.cmd:
    Code:
    @echo off
    if "%1"=="" Goto :help
    
    for /f "tokens=2 delims={}" %%g in ('bcdedit.exe /create /d %1 /Device') do (set dvid={%%g})
    bcdedit /set %dvid% ramdisksdidevice partition=%2
    bcdedit /set %dvid% ramdisksdipath %4
    for /f "tokens=2 delims={}" %%g in ('bcdedit.exe /create /d %1 /application osloader') do (set guid={%%g})
    bcdedit /set %guid% device ramdisk=[%2]%3,%dvid%
    bcdedit /set %guid% osdevice ramdisk=[%2]%3,%dvid%
    bcdedit /set %guid% systemroot \windows
    bcdedit /set %guid% path \Windows\system32\boot\winload.%5
    bcdedit /set %guid% winpe yes
    bcdedit /set %guid% detecthal yes
    bcdedit /displayorder %guid% /addlast
    bcdedit /set {bootmgr} displaybootmenu True
    bcdedit /set {bootmgr} timeout 5
    goto :Eof
    
    :help
    echo.
    echo Usage: abo Name Drive: \Path\Boot.wim \Path\Boot.sdi efi/exe
    echo.

    Ex:
    Code:
    abo Reflect8 D: \BootImages\Reflect8\Boot.wim \BootImages\Reflect8\Boot.sdi efi
    Will create a EFI Boot entry for Reflect8

    Limitations:
    Does not support "space" in Name.
    If there are spaces in paths, they must be "in quotes"

    Basically, I would like to add VHD capability to it.

    Thanks !!!
    One way is create a diskpart text file say diskpart.txt like

    Code:
    select vdisk file="E:\Hyper-V\Virtual Hard Disks\W11HD.vhdx" 
    attach vdisk
    select volume n   (where n is volume of windows partition in vhdx file)
    assign letter=m  (use a letter not normally used and away from lower letters)
    Then in batch file have a command

    Code:
    diskpart /s diskpart.txt
    then add boot entry

    Code:
    bcdboot m:\windows /d
    I actually create the diskpart.txt file from batch file using @echo, and various parameters set up earlier.

    In this example extracted from a batch file of mine, I add the commands to create a vhd, select and attach vhd, convert vhd to gpt, create and format a primary partition, format it and add a drive letter.

    I later install windows using dism, and add boot entry.


    Code:
    @echo create vdisk file="%VDRV%:%Hyperfiles%\%vhdname%.vhdx" maximum=%Maxvhdsize% type=expandable > %DRV%:%Updateiso%\diskpart\diskpart.txt
    @echo select vdisk file="%VDRV%:%Hyperfiles%\%vhdname%.vhdx" >> "%DRV%:%Updateiso%\diskpart\diskpart.txt"
    @echo attach vdisk >> "%DRV%:%Updateiso%\diskpart\diskpart.txt"@echo convert gpt >> "%DRV%:%Updateiso%\diskpart\diskpart.txt"
    @echo create partition primary >> "%DRV%:%Updateiso%\diskpart\diskpart.txt"
    @echo format fs=ntfs quick label="Windows" >> "%DRV%:%Updateiso%\diskpart\diskpart.txt"@echo assign letter=%VHDdrive% >> "%DRV%:%Updateiso%\diskpart\diskpart.txt"
      My Computer


  3. Posts : 2,651
    Windows 7 Pro
    Thread Starter
       #3

    Thanks for your answer @cereberus,

    For my test I made a small vhdx with the Microsoft method and applied a windows install image on it...

    All I needed was a way to add it to boot menu without having to attach it etc...

    But I finally got it rolling:
    Code:
    for /f "tokens=2 delims={}" %%g in ('bcdedit.exe /create /d %2 /Device') do (set dvid={%%g})
    bcdedit /set %dvid% device vhd=[%3]%4
    for /f "tokens=2 delims={}" %%g in ('bcdedit.exe /create /d %2 /application osloader') do (set guid={%%g})
    bcdedit /set %guid% device vhd=[%3]%4,%dvid%
    bcdedit /set %guid% osdevice vhd=[%3]%4,%dvid%
    bcdedit /set %guid% systemroot \windows
    bcdedit /set %guid% path \Windows\system32\boot\winload.%5
    bcdedit /set %guid% winpe no
    bcdedit /set %guid% detecthal yes
    bcdedit /displayorder %guid% /addlast
    bcdedit /set {bootmgr} displaybootmenu True
    bcdedit /set {bootmgr} timeout 5
    Finally a working VHDX was added !

    I updated the OP with the final working code.
      My Computers


  4. Posts : 11,210
    Windows / Linux : Arch Linux
       #4

    MaloK said:
    Hi,

    This CMD File can be used to easily add boot entries for VHD(x) and WIM images. With one command.

    abo.cmd:
    Code:
    @echo off
    if "%1"=="" Goto :help
    if "%1"=="ramdisk" goto ramdisk
    if "%1"=="vhd" goto vhd
    
    :ramdisk
    for /f "tokens=2 delims={}" %%g in ('bcdedit.exe /create /d %2 /Device') do (set dvid={%%g})
    bcdedit /set %dvid% ramdisksdidevice partition=%3
    bcdedit /set %dvid% ramdisksdipath %5
    for /f "tokens=2 delims={}" %%g in ('bcdedit.exe /create /d %2 /application osloader') do (set guid={%%g})
    bcdedit /set %guid% device ramdisk=[%3]%4,%dvid%
    bcdedit /set %guid% osdevice ramdisk=[%3]%4,%dvid%
    bcdedit /set %guid% systemroot \windows
    bcdedit /set %guid% path \Windows\system32\boot\winload.%6
    bcdedit /set %guid% winpe yes
    bcdedit /set %guid% detecthal yes
    bcdedit /displayorder %guid% /addlast
    bcdedit /set {bootmgr} displaybootmenu True
    bcdedit /set {bootmgr} timeout 5
    goto :Eof
    
    :vhd
    for /f "tokens=2 delims={}" %%g in ('bcdedit.exe /create /d %2 /Device') do (set dvid={%%g})
    bcdedit /set %dvid% device vhd=[%3]%4
    for /f "tokens=2 delims={}" %%g in ('bcdedit.exe /create /d %2 /application osloader') do (set guid={%%g})
    bcdedit /set %guid% device vhd=[%3]%4,%dvid%
    bcdedit /set %guid% osdevice vhd=[%3]%4,%dvid%
    bcdedit /set %guid% systemroot \windows
    bcdedit /set %guid% path \Windows\system32\boot\winload.%5
    bcdedit /set %guid% winpe no
    bcdedit /set %guid% detecthal yes
    bcdedit /displayorder %guid% /addlast
    bcdedit /set {bootmgr} displaybootmenu True
    bcdedit /set {bootmgr} timeout 5
    goto :Eof
    
    :help
    echo.
    echo Usage: abo ramdisk Name Drive: \Path\Boot.wim \Path\Boot.sdi efi/exe
    echo Usage: abo vhd Name Drive: \Path\Boot.vhdx efi/exe
    echo.

    Ex:
    Code:
    abo ramdisk Reflect8 D: \BootImages\Reflect8\Boot.wim \BootImages\Reflect8\Boot.sdi efi
    Will create a EFI Boot entry for Reflect8 Ramdisk

    Ex:
    Code:
    abo vhd Win10Pro D: \Win10Pro.vhdx exe
    Will create a MBR Boot entry for Windows10 pro VHD

    Limitations:
    Does not support "space" in Name.
    If there are spaces in paths, they must be "in quotes"

    Enjoy !!!
    Hi there

    How about also being able to optionally add the relevant boot files if booting from physical vhdx files -- e.g typical statement : bcdboot w:\windows /s S /f UEFI -Be nice to parameterize that IMO.

    where W is the vhdx attached file for "target windows install" s is the efi partition of the physical disk and /UEFI or /MBR is for whether the vhdx file should be booted in EFI or MBR mode. If using W11 it needs to be /UEFI since W11 wants sec boot these days.

    Another good one would also be able to parameterise dism.exe /Apply-Image etc as it's so easy to make stupid errors when typing it all out.

    I can do bash scripts easy enough but Windows scripts -- etc !!!! I pass over to others with superior knowlege.

    Cheers
    jimbo
      My Computer


  5. Posts : 2,651
    Windows 7 Pro
    Thread Starter
       #5

    @jimbo45,

    After your Little suggestion and a little more fiddling,

    I was able to create a reliable CMD file that is doing it exactly the Microsoft way, in both BIOS and UEFI.

    Flat.cmd:
    Code:
    ::###########################################################################
    ::# Title     : Flat.cmd                                                    #
    ::# Created By: https://www.tenforums.com/members/malok.html                #
    ::# Created On: 15-Jun-2022                                                 #
    ::###########################################################################
    
    @Echo Off
    Set "Params=%1"
    If "%Params%"=="" Goto :Help
    
    Diskpart /s %~dp0\%3.txt
    
    Dism /Apply-Image /ImageFile:%1 /Index:%2 /ApplyDir:W:\
    
    W:\Windows\System32\bcdboot W:\Windows /s S: /f %3
    
    Md R:\Recovery\WindowsRE
    Xcopy /h W:\Windows\System32\Recovery\Winre.wim R:\Recovery\WindowsRE\
    
    W:\Windows\System32\Reagentc /Setreimage /Path R:\Recovery\WindowsRE /Target W:\Windows
    
    W:\Windows\System32\Reagentc /Info /Target W:\Windows
    
    Goto :Eof
    
    :Help
    Echo.
    Echo ======================== Warning =========================
    Echo.
    Echo This CMD file is used to Ininitalize / Clean hardisk 0.
    Echo Automatically apply a Windows image and Create Boot files.
    Echo EFI and MBR txt files should be reviewed and edited if needed.
    Echo.
    Echo flat {source} {Index#} {bios/uefi}
    Echo.
    Echo flat e:\Sources\Install.wim 6 uefi
    Echo.

    UEFI.txt:
    Code:
    rem == CreatePartitions-UEFI.txt ==
    rem == These commands are used with DiskPart to
    rem    create four partitions
    rem    for a UEFI/GPT-based PC.
    rem    Adjust the partition sizes to fill the drive
    rem    as necessary. ==
    select disk 0
    clean
    convert gpt
    
    rem == 1. System partition =========================
    create partition efi size=100
    rem    ** NOTE: For Advanced Format 4Kn drives,
    rem               change this value to size = 260 ** 
    format quick fs=fat32 label="System"
    assign letter="S"
    
    rem == 2. Microsoft Reserved (MSR) partition =======
    create partition msr size=16
    
    rem == 3. Windows partition ========================
    rem ==    a. Create the Windows partition ==========
    create partition primary 
    
    rem ==    b. Create space for the recovery tools ===
    rem       ** Update this size to match the size of
    rem          the recovery tools (winre.wim)
    rem          plus some free space.
    shrink minimum=500
    
    rem ==    c. Prepare the Windows partition ========= 
    format quick fs=ntfs label="Windows"
    assign letter="W"
    
    rem === 4. Recovery partition ======================
    create partition primary
    format quick fs=ntfs label="Recovery"
    assign letter="R"
    set id="de94bba4-06d1-4d40-a16a-bfd50179d6ac"
    gpt attributes=0x8000000000000001
    
    list volume
    exit

    BIOS.txt:
    Code:
    rem == CreatePartitions-BIOS.txt ==
    rem == These commands are used with DiskPart to
    rem    create three partitions
    rem    for a BIOS/MBR-based computer.
    rem    Adjust the partition sizes to fill the drive
    rem    as necessary. ==
    select disk 0
    clean
    
    rem == 1. System partition ======================
    create partition primary size=100
    format quick fs=ntfs label="System"
    assign letter="S"
    active
    
    rem == 2. Windows partition =====================
    rem ==    a. Create the Windows partition =======
    create partition primary
    
    rem ==    b. Create space for the recovery tools  
    rem       ** Update this size to match the size of
    rem          the recovery tools (winre.wim)
    rem          plus some free space.
    shrink minimum=650
    
    rem ==    c. Prepare the Windows partition ====== 
    format quick fs=ntfs label="Windows"
    assign letter="W"
    
    rem == 3. Recovery tools partition ==============
    create partition primary
    format quick fs=ntfs label="Recovery"
    assign letter="R"
    set id=27
    
    list volume
    exit

    WARNING

    This CMD file Will DELETE ALL PARTITIONS on Drive 0 Without questions. Of course you can edit the configuration txt file to apply to any disk you want, you need Drive letters R, S and W to be free to run the command.

    The UEFI File should be modified accordingly if you are using 4Kn Disks.

    Transcript of an iteration:
    Code:
    E:\Flat>flat d:\sources\install.wim 6 uefi
    
    Microsoft DiskPart version 10.0.19041.964
    
    Copyright (C) Microsoft Corporation.
    On computer: MINWINPC
    
    Disk 0 is now the selected disk.
    
    DiskPart succeeded in cleaning the disk.
    
    DiskPart successfully converted the selected disk to GPT format.
    
    DiskPart succeeded in creating the specified partition.
    
      100 percent completed
    
    DiskPart successfully formatted the volume.
    
    DiskPart successfully assigned the drive letter or mount point.
    
    DiskPart succeeded in creating the specified partition.
    
    DiskPart succeeded in creating the specified partition.
    
    DiskPart successfully shrunk the volume by:  500 MB
    
      100 percent completed
    
    DiskPart successfully formatted the volume.
    
    DiskPart successfully assigned the drive letter or mount point.
    
    DiskPart succeeded in creating the specified partition.
    
      100 percent completed
    
    DiskPart successfully formatted the volume.
    
    DiskPart successfully assigned the drive letter or mount point.
    
    DiskPart successfully set the partition ID.
    
    DiskPart successfully assigned the attributes to the selected GPT partition.
    
      Volume ###  Ltr  Label        Fs     Type        Size     Status     Info
      ----------  ---  -----------  -----  ----------  -------  ---------  --------
      Volume 0     D   CCCOMA_X64F  UDF    CD-ROM      5611 MB  Healthy
      Volume 1     W   Windows      NTFS   Partition     49 GB  Healthy
      Volume 2     S   SYSTEM       FAT32  Partition    100 MB  Healthy    Hidden
    * Volume 3     R   Recovery     NTFS   Partition    500 MB  Healthy    Hidden
      Volume 4     E   CCCOMA_X64F  NTFS   Removable     14 GB  Healthy
      Volume 5     F   UEFI_NTFS    FAT    Removable   1024 KB  Healthy
    
    Leaving DiskPart...
    
    Deployment Image Servicing and Management tool
    Version: 10.0.19041.844
    
    Applying image
    [==========================100.0%==========================]
    The operation completed successfully.
    Boot files successfully created.
    W:\Windows\System32\Recovery\Winre.wim
    1 File(s) copied
    Directory set to: \\?\GLOBALROOT\device\harddisk0\partition4\Recovery\WindowsRE
    
    REAGENTC.EXE: Operation Successful.
    
    Windows Recovery Environment (Windows RE) and system reset configuration
    Information:
    
        Windows RE status:         Disabled
        Windows RE location:
        Boot Configuration Data (BCD) identifier: 00000000-0000-0000-0000-000000000000
        Recovery image location:
        Recovery image index:      0
        Custom image location:
        Custom image index:        0
    
    REAGENTC.EXE: Operation Successful.
    
    E:\Flat>

    The last output about Windows RE being disabled seems a little confusing. But everything is good upon booting the restored system.
    CMD file to add a VHD(x) boot object in BCD Attached Files
    Last edited by MaloK; 1 Week Ago at 16:17.
      My Computers


  6. Posts : 13,695
    Windows10
       #6

    Re. vhdx, why not just mount it as a drive, note drive letter of windows folder (say F) and type

    bcdboot f:\windows /p /d.

    Re. boot.wim boot entries @Kyhi has a great one in his post on win10XPE.

    I would reproduce it here with his permission.
      My Computer


  7. Posts : 3,865
    Windows 3.1 to Windows 10
       #7

    Go for it Cereberus - open source....
      My Computer


  8. Posts : 13,695
    Windows10
       #8

    Kyhi said:
    Go for it Cereberus - open source....
    Cheers.

    So mount partition containing boot files, and then note drive letter and run following batch file (save contents in.cmd file).

    Configure line at beginning as you see fit.

    Rem - change Wimname to what ever you want boot entry to be shown as e.g. Macrium ReflectRem - Set Wimname=Macrium ReflectSet Wimname=My Wim Boot Entry

    -------------------------------------------------------------------------------------------------------------------------------------------------------------


    Code:
    @echo off
    
    
    title Wimboot Menu creator
    
    
    Rem - change Wimname to what ever you want boot entry to be shown as e.g. Macrium Reflect
    Rem - Set Wimname=Macrium Reflect
    
    
    Set Wimname=My Wim Boot Entry
    
    
    rem ================================ YY.MM.DD
    
    
    color 17
    
    
    :start
    
    
    cls
    
    
    echo.
    
    
    echo ===============================================================================
    
    
    echo.  YOU HAVE CHOOSEN TO ADD A WINPESE BOOTABLE PARTITION TO YOUR HOST
    
    
    echo ===============================================================================
    
    
    echo.
    
    
    set /p drvlttr= ^> ENTER THE DRIVE LETTER ASSIGNED TO THE WINPESE PARTITION: 
    
    
    if [%drvlttr%]==[] echo.&echo Invalid User Input&echo.&echo The Partition Drive Letter Can Not Be Blank&echo.&pause&goto :start
    
    
    if not exist %drvlttr%:\sources\boot.wim echo.&echo Invalid User Input&echo.&echo The %drvlttr%:\sources\boot.wim Was Not Found&echo.&pause&goto :start
    
    
    cls
    
    
    echo.
    
    
    echo Configuring UEFI Boot Option for WinPESE Recovery Tools
    
    
    echo --------------------------------------------------------------------------
    
    
    for /f "tokens=2 delims={}" %%g in ('bcdedit.exe /create /d "%Wimname" /Device') do (set deviceid={%%g})
    
    
    echo --------------------------------------------------------------------------
    
    
    echo DEVICE ID = %device
    
    
    echo --------------------------------------------------------------------------
    
    
    bcdedit /set %deviceid% ramdisksdidevice partition=%drvlttr%:
    
    
    bcdedit /set %deviceid% ramdisksdipath \boot\boot.sdi
    
    
    for /f "tokens=2 delims={}" %%g in ('bcdedit.exe /create /d "%Wimname" /application osloader') do (set guid={%%g})
    
    
    echo --------------------------------------------------------------------------
    
    
    echo GUID = %guid%
    
    
    echo --------------------------------------------------------------------------
    
    
    bcdedit /set %guid% device ramdisk=[%drvlttr%:]\sources\boot.wim,%deviceid%
    
    
    bcdedit /set %guid% osdevice ramdisk=[%drvlttr%:]\sources\boot.wim,%deviceid%
    
    
    bcdedit /set %guid% systemroot \windows
    
    
    bcdedit /set %guid% path \Windows\system32\boot\winload.efi
    
    
    bcdedit /set %guid% winpe yes
    
    
    bcdedit /set %guid% detecthal yes
    
    
    bcdedit /displayorder %guid% /addlast
    
    
    bcdedit /set {bootmgr} displaybootmenu False
    
    
    bcdedit /set {bootmgr} timeout 10
    
    
    echo.
    
    
    echo DONE... Reboot your PC
    
    
    timeout /t 5 >nul
      My Computer


  9. Posts : 2,651
    Windows 7 Pro
    Thread Starter
       #9

    @cereberus, thanks for your participation

    That seems to sums it... I used flat.cmd on VHDs then abo.cmd to add them to my BCD and all was good here...

    I wanted to leave both CMDs separated because Flat.cmd can be used on any non removable destinations... abo.cmd will only work with your current BCD...

    I left the vhds creation out, because it could also be any existing disk with already partitions on them.

    Of course I could consolidate every single options to be provided on the command lines... But It would require the cmd file to write the Diskpart Txts before applying...

    After validating against Kyhi's Script, I believe that abo.cmd is doing it right. But maybe Flat.cmd could have an option to only restore the Windows partition without creating boot files and Supp partitions. So that Plain VHDs could be booted natively.

    At the moment I use flat to restore images offline then put back the drive in the client machine and boot directly to desktop in one command.

    Note that this is not really Home user oriented... I admit.

    But it does create all boot partitions and files on the "Right" drive without having to unplug anything.
      My Computers


  10. Posts : 3,865
    Windows 3.1 to Windows 10
       #10

    Since I am late to the topic
    1) Create the VHD
    2) Apply OS Image
    3) Create Boot Entry (either Host or Stand alone)
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

Designer Media Ltd
All times are GMT -5. The time now is 06:54.
Find Us




Windows 10 Forums