murphy78 DiskPart and Apply Image Script

Discussion in 'Scripting' started by murphy78, Apr 2, 2014.

  1. murphy78

    murphy78 MDL DISM Enthusiast

    Nov 18, 2012
    7,466
    11,872
    240
    Sorry @Low Level Perform and @Enthousiast , I've been extremely busy lately. There is an issue with wmic commands not working on windows 11 LTSC?
    I modified Enth's script installer and from what I can tell, all this wmic line does is check the date and add it to the iso. I will see if I can look into it tomorrow without any powershell or wmic commands to simplify while keeping the functionality.
     
  2. Carlos Detweiller

    Carlos Detweiller Emperor of Ice-Cream
    Staff Member

    Dec 21, 2012
    8,076
    10,298
    270
    Microsoft deprecated WMIC.EXE and it is not installed by default on Windows 11 OS 24H2 or newer. VBScript will be next.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. Enthousiast

    Enthousiast MDL Tester

    Oct 30, 2009
    54,966
    125,937
    450
    We had to change all our scripts when they ripped out wmic as a test (iirc) but they re-inserted it then but the scripts were already modernized, so it was kept that way :)

    It's just a one line replacement with PS:
    https://forums.mydigitallife.net/posts/1901512
    I also changed the iso filename buildup in my script so it takes the original filename and adds _isodate.iso (not in the post i linked at)
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  4. murphy78

    murphy78 MDL DISM Enthusiast

    Nov 18, 2012
    7,466
    11,872
    240
    Here you go, @Low Level Perform
    You can save this code and put it in the same folder as the v1.3.2 add-diskpart script. It will check to see if powershell is available and run it. Then it will check to see if wmic is available and run that. If all else fails it will use the internal date. If nothing works, it will set date code to 20xx.
    I tested it on a VM using win 11 and on my win10 machine.
    Updated add-Diskpart_Apply_Image_Tool_murphy78_custom_v1.3.3.cmd :
    Code:
    @echo off
    setlocal EnableDelayedExpansion
    color 1f
    REM change wording if needed..
    TITLE Admin Check...
    echo Checking for admin...
    
    if not "%1"=="am_admin" (powershell start -verb runas '%0' am_admin & exit /b)
    
    :gotAdmin
        pushd "%CD%"
        CD /D "%~dp0"
    cls
    @echo off
    TITLE Add Murphy78 Diskpart and Apply Image script to boot.wim originally by Enthousiast...
    
    setlocal EnableDelayedExpansion
    echo ============================================================
    echo Add Muprhy78 Diskpart and Apply Image script to boot.wim
    echo originally by Enthousiast v1.3.3...
    echo ============================================================
    
    
    :: Detect OS Architecture
    if %processor_architecture% == AMD64 (set arch=x64) else (set arch=x86)
    
    if %arch%==x86 (
      set _wimlib="bin\wimlib-imagex.exe"
      set "xOS=x86"
    ) else (
      set _wimlib="bin\bin64\wimlib-imagex.exe"
      set "xOS=amd64"
    )
    
    if exist "Work" rmdir /q /s "WORK"
    if exist "TEMP" rmdir /q /s "TEMP"
    if not exist "Source_ISO\*.iso" (
    echo.
    ECHO==========================================================
    echo No iso file detected in Source_ISO dir...
    ECHO==========================================================
    echo.
    pause
    exit /b
    )
    
    Set DPFOLDER=murphy78-DiskPart-Apply-v1.5
    Set BOOTWIMINDEX=2
    if not exist %DPFOLDER%\winpeshl.ini (
    echo winpeshl.ini not found
    pause
    goto :EOF
    )
    echo.
    echo An alternate winpeshl.ini can auto-run the diskpart/apply image script
    echo Would you like to replace the file in setup media?
    
    choice /c ynq /n /m "Y/N or Q to quit"
    SET ERRORTEMP=!ERRORLEVEL!
    IF !ERRORTEMP! EQU 3 (goto :EOF)
    IF !ERRORTEMP! EQU 2 (SET COPYCMD=0)
    IF !ERRORTEMP! EQU 1 (SET COPYCMD=1)
    
    mkdir "TEMP"
    for /f %%i in ('dir /b Source_ISO\*.iso') do bin\7z.exe e -y -oTEMP Source_ISO\%%i sources\setup.exe >nul
    bin\7z.exe l .\TEMP\setup.exe >.\TEMP\version.txt 2>&1
    for /f "tokens=4 delims=. " %%i in ('findstr /i /b FileVersion .\TEMP\version.txt') do set vermajor=%%i
    for /f "tokens=4,5 delims=. " %%i in ('findstr /i /b FileVersion .\TEMP\version.txt') do (set majorbuildnr=%%i&set deltabuildnr=%%j)
    
    IF DEFINED vermajor goto :vermajorsuccess
    
    if exist "TEMP" rmdir /q /s "TEMP"
    
    mkdir "TEMP"
    for /f %%i in ('dir /b Source_ISO\*.iso') do bin\7z.exe e -y -oTEMP Source_ISO\%%i x64\sources\setup.exe >nul
    bin\7z.exe l .\TEMP\setup.exe >.\TEMP\version.txt 2>&1
    for /f "tokens=4 delims=. " %%i in ('findstr /i /b FileVersion .\TEMP\version.txt') do set vermajor=%%i
    for /f "tokens=4,5 delims=. " %%i in ('findstr /i /b FileVersion .\TEMP\version.txt') do (set majorbuildnr=%%i&set deltabuildnr=%%j)
    
    IF NOT DEFINED vermajor (
    if exist "TEMP" rmdir /q /s "TEMP"
    
    echo.
    ECHO==========================================================
    echo Detecting setup.exe version failed...
    ECHO==========================================================
    echo.
    pause
    exit /b
    )
    
    :vermajorsuccess
    echo.
    echo Extracting Source ISO...
    bin\7z x -y -oWork\ Source_ISO\
    
    REM detect wim arch
    if not exist Work\sources\install.wim goto :detectbuildesd
    for /f "tokens=2 delims=: " %%# in ('dism.exe /english /get-wiminfo /wimfile:"Work\sources\install.wim" /index:1 ^| find /i "Architecture"') do set warch=%%#
    for /f "tokens=3 delims=: " %%m in ('dism.exe /english /Get-WimInfo /wimfile:"Work\sources\install.wim" /Index:1 ^| findstr /i Build') do set b2=%%m&goto :detectisolang
    :detectbuildesd
    if not exist Work\sources\install.esd goto :detectbuildswm
    for /f "tokens=2 delims=: " %%# in ('dism.exe /english /get-wiminfo /wimfile:"Work\sources\install.esd" /index:1 ^| find /i "Architecture"') do set warch=%%#
    for /f "tokens=3 delims=: " %%m in ('dism.exe /english /Get-WimInfo /wimfile:"Work\sources\install.esd" /Index:1 ^| findstr /i Build') do set b2=%%m&goto :detectisolang
    :detectbuildswm
    if not exist Work\sources\install.swm goto :detectbuilddual
    for /f "tokens=2 delims=: " %%# in ('dism.exe /english /get-wiminfo /wimfile:"Work\sources\install.swm" /index:1 ^| find /i "Architecture"') do set warch=%%#
    for /f "tokens=3 delims=: " %%m in ('dism.exe /english /Get-WimInfo /wimfile:"Work\sources\install.swm" /Index:1 ^| findstr /i Build') do set b2=%%m&goto :detectisolang
    
    :detectbuilddual
    set warch=x86_x64
    if not exist "Work\x64\sources\install.wim" goto :detectdualesd
    for /f "tokens=3 delims=: " %%m in ('dism.exe /english /Get-WimInfo /wimfile:"Work\x64\sources\install.wim" /Index:1 ^| findstr /i Build') do set b2=%%m&goto :detectisolang
    
    :detectdualesd
    if not exist "Work\x64\sources\install.esd" goto :detectdualswm
    for /f "tokens=3 delims=: " %%m in ('dism.exe /english /Get-WimInfo /wimfile:"Work\x64\sources\install.esd" /Index:1 ^| findstr /i Build') do set b2=%%m&goto :detectisolang
    
    :detectdualswm
    if not exist "Work\x64\sources\install.swm" goto :cannotfindinstall
    for /f "tokens=3 delims=: " %%m in ('dism.exe /english /Get-WimInfo /wimfile:"Work\x64\sources\install.swm" /Index:1 ^| findstr /i Build') do set b2=%%m
    
    :detectisolang
    REM detect extracted iso language
    set "IsoLang=ar-SA,bg-BG,cs-CZ,da-DK,de-DE,el-GR,en-GB,en-US,es-ES,es-MX,et-EE,fi-FI,fr-CA,fr-FR,he-IL,hr-HR,hu-HU,it-IT,ja-JP,ko-KR,lt-LT,lv-LV,nb-NO,nl-NL,pl-PL,pt-BR,pt-PT,ro-RO,ru-RU,sk-SK,sl-SI,sr-RS,sv-SE,th-TH,tr-TR,uk-UA,zh-CN,zh-TW"
    if not exist Work\sources goto :detectlangdual
    for %%i in (%IsoLang%) do if exist "Work\sources\%%i\*.mui" set %%i=1&goto :isolang
    
    :detectlangdual
    for %%i in (%IsoLang%) do if exist "Work\x64\sources\%%i\*.mui" set %%i=1
    
    :isolang
    REM set ISO label lang
    for %%i in (%IsoLang%) do if defined %%i (
    SET "LabelLang=%%i"
    )
    
    echo.
    echo Adding Murphy78 Diskpart and Apply Image Script To Boot.wim...
    echo.
    if not exist Work\sources\boot.wim goto :murphydualboot
    %_wimlib% update Work\sources\boot.wim %BOOTWIMINDEX% --command "add '%DPFOLDER%\%warch%\' '\'"
    %_wimlib% optimize "WORK\Sources\boot.wim" --recompress&goto :startwinpeshl
    :murphydualboot
    %_wimlib% update Work\x64\sources\boot.wim %BOOTWIMINDEX% --command "add '%DPFOLDER%\x64\' '\'"
    %_wimlib% optimize "WORK\x64\Sources\boot.wim" --recompress
    %_wimlib% update Work\x86\sources\boot.wim %BOOTWIMINDEX% --command "add '%DPFOLDER%\x86\' '\'"
    %_wimlib% optimize "WORK\x86\Sources\boot.wim" --recompress
    
    :startwinpeshl
    if !COPYCMD! EQU 0 (goto :startiso)
    echo.
    echo ============================================================
    echo Replacing the winpeshl.ini - Script will run automatically
    echo ============================================================
    echo.
    if not exist Work\sources\boot.wim goto :winpedualboot
    %_wimlib% update Work\sources\boot.wim %BOOTWIMINDEX% --command "add '%DPFOLDER%\winpeshl.ini' 'Windows\System32\winpeshl.ini'"
    %_wimlib% optimize "WORK\Sources\boot.wim" --recompress&goto :startiso
    :winpedualboot
    %_wimlib% update Work\x64\sources\boot.wim %BOOTWIMINDEX% --command "add '%DPFOLDER%\winpeshl.ini' 'Windows\System32\winpeshl.ini'"
    %_wimlib% optimize "WORK\x64\Sources\boot.wim" --recompress
    %_wimlib% update Work\x86\sources\boot.wim %BOOTWIMINDEX% --command "add '%DPFOLDER%\winpeshl.ini' 'Windows\System32\winpeshl.ini'"
    %_wimlib% optimize "WORK\x86\Sources\boot.wim" --recompress
    
    :startiso
    echo.
    ECHO==========================================================
    echo Creating  %WARCH% ISO...
    ECHO==========================================================
    echo.
    set "isodate="
    rem === Try PowerShell only if it exists ===
    if exist "%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" (
        for /f "delims=" %%# in ('
            cmd /c "%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -Command Get-Date -Format yyyy-MM-dd"
        ') do set "isodate=%%#"
    )
    rem === Try WMIC only if it exists ===
    if not defined isodate if exist "%SystemRoot%\System32\wbem\wmic.exe" (
        for /f "tokens=2 delims==." %%# in ('
            cmd /c "%SystemRoot%\System32\wbem\wmic.exe os get localdatetime /value"
        ') do (
            set "_date=%%#"
            set "isodate=!_date:~0,4!-!_date:~4,2!-!_date:~6,2!"
        )
    )
    rem --- Final fallback: %DATE% (locale-dependent)
    if not defined isodate (
        rem Assumes YYYY-MM-DD, YYYY/MM/DD, or MM/DD/YYYY
        for /f "tokens=1-3 delims=/.-" %%a in ("%DATE%") do (
            rem Heuristic: if first token is 4 digits, assume YYYY
            if "%%a" GEQ "1900" (
                set "isodate=%%a-%%b-%%c"
            ) else (
                set "isodate=%%c-%%a-%%b"
            )
        )
    )
    rem --- Safety check
    if not defined isodate (
        echo ERROR: Unable to determine system date
        set "isodate=20xx"
        pause
    )
    echo.
    if not exist "Work\efi\Microsoft\boot\efisys.bin" goto makenoneficd
    :makeeficd
    bin\cdimage.exe -bootdata:2#p0,e,b"Work\boot\etfsboot.com"#pEF,e,b"Work\efi\Microsoft\boot\efisys.bin" -o -m -u2 -udfver102 -lWin_%vermajor%_%warch%_%LabelLang% Work Win_%majorbuildnr%.%b2%_%wARCH%_%LabelLang%_%isodate%.iso
    goto cleanup
    
    :makenoneficd
    echo No efisys.bin detected, attempting to make non-UEFI compatible iso
    bin\cdimage.exe -b"Work\boot\etfsboot.com" -o -m -u2 -udfver102 -lWin_%vermajor%_%warch%_%LabelLang% Work Win_%majorbuildnr%.%b2%_%wARCH%_%LabelLang%_%isodate%.iso
    if %ERRORLEVEL% NEQ 0 echo Errors reported by dvd imaging tool
    
    :cleanup
    if exist "TEMP" rmdir /q /s "TEMP"
    if exist "Work" rmdir /q /s "WORK"
    
    pause
    exit /b
    
    :cannotfindinstall
    Echo This script only supports install.wim and install.esd format
    Echo Structure must be sources\install.wim or x64\sources\install.wim
    pause
    exit /b
    
    :EOF
     
  5. Low Level Perform

    Low Level Perform MDL Member

    Jul 21, 2024
    154
    124
    10
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  6. Vachira

    Vachira MDL Novice

    Apr 24, 2026
    3
    1
    0
  7. murphy78

    murphy78 MDL DISM Enthusiast

    Nov 18, 2012
    7,466
    11,872
    240
    I have a small update to v1.5v2
    Changed the add Diskpart Apply Image tool custom script
    It's been bugging me that I didn't release a new version for the people who want to use this on win 11 without dealing with making a separate script
    If you have saved the code from above as 1.3.3, there is nothing else new except an updated compilation

    Changelog v1.5v2:
    Swapped the add-Diskpart_Apply_Image_Tool_murphy78_custom script to the updated 1.3.3 version
    This version should update the generated ISO filename correctly on Windows 11 and possibly beyond

    Get the File:
    New:

    https://mir.cr/72DFDDD2
    File Name: murphy78-Diskpart-Apply-v1.5v2.7z
    File Size: 4.59 MB
    SHA1: 4839C818B3A4DF8D3AF35E5A237ED2E61F7ABDD9
    File type: 7z-LZMA/pw
    PW: MDL2026


    Compilation:
    https://mir.cr/1598F1E9
    File Name: murphy78-Diskpart-Apply-Script_v1.2.8-to-v1.5v2-compilation.7z
    File Size: 52.8 MB
    SHA1: FBEC8F92BD506A9D5DF08F5DE92A411F7536B1C1
    File type: 7z-LZMA/pw
    PW: MDL2026
     
  8. fabricio simoes

    fabricio simoes MDL Senior Member

    Dec 17, 2020
    331
    90
    10
    It's a shame no one like me doesn't know how to translate it into Brazilian Portuguese. :( . . .
     
  9. fabricio simoes

    fabricio simoes MDL Senior Member

    Dec 17, 2020
    331
    90
    10
    It's on the installer loading 100% screen, it doesn't change, is it no longer a script?
    [​IMG]
     
  10. murphy78

    murphy78 MDL DISM Enthusiast

    Nov 18, 2012
    7,466
    11,872
    240
    My goal with this script was to imitate Microsoft's setup process, but having support for esd, wim, and swm format. Normally, the Microsoft Windows setup only supports esd and wim formats.
    If you try to use swm split-wim format, it will look for the appropriate EULA and generally fail if not found. This is not an issue if you only have one large captured image, but it is very frustrating for an AIO.
    I never imagined that I would be dealing with multiple languages and Win 11 compatibility issues.

    The main issue with translating into Brazilian Portuguese, or anything other than English, is that there are many different little programs that are required for the script to function.
    There's like 10 or 20 different commands that might have different functionality in other languages. Some might require English commands but display output in another language.
    Changing the commands that display different language often break the functionality of the script. This is why I had to force English for Diskpart.
    It is possible that you could rewrite that part to be specific to Brazilian Portuguese, but I wouldn't know how to do it as I don't speak the language.

    If you were seriously considering this, I should warn you that it would probably take you a year to complete and it might not be worth it.
    Little tiny changes in the script are extremely hard to bug fix and they're even harder if you weren't the one who wrote the script.
    You might start with doing one small command at a time though and using it on a vm to see how it works.

    I don't know what you mean by installer loading screen. If you are talking about what you got from mirrorcreator you should be warned that they have one of those bogus download things.
    Make sure you download the exact file that matches the sha1 hash. The first download is fake. you have to close that window and reclick the link.
     
  11. fabricio simoes

    fabricio simoes MDL Senior Member

    Dec 17, 2020
    331
    90
    10

    I didn't understand what this exe was going to do, I downloaded both of them and ran them and it stops at this screen that I sent you in the printout? I don't know how to do it I just ran exe???
     
  12. fabricio simoes

    fabricio simoes MDL Senior Member

    Dec 17, 2020
    331
    90
    10
    #352 fabricio simoes, May 19, 2026
    Last edited: May 19, 2026
    I found myself here really failing, I was downloading the wrong file, I hope this exe is not malicious, in python, now I downloaded the script, but I really wanted Brazilian Portuguese, because I don't understand English and I can format the disk wrong. . .

    [​IMG]

    Diskpart_Apply_Image_Tool_murphy_custom_v1.zip

    Incorrect password?
     
  13. murphy78

    murphy78 MDL DISM Enthusiast

    Nov 18, 2012
    7,466
    11,872
    240
    You can always use the Autoruns64 tool to see if it installed any kind of malware and disable it. On the Brazilian Portuguese issue, really the main that you need to worry about is selecting the correct disk. If it's hard to understand, there are apps you can download on your phone and take a picture to auto-translate I believe. That would certainly be a LOT easier than rewriting the entire script. Basically the script defaults to disk 0 which is the first disk that the motherboard assigns. That can often be an SSD or a hard disk instead of an NVME SSD though. This is why I have the Disk Select function. This shows the various DISK sizes to make it easier to locate the appropriate DISK NUMBER. Say you have a SSD with 500GB and an NVME with 2TB.
    For example:
    Code:
      Disk       ###           Status           Size             Free     Dyn  Gpt
      --------    ------------   -------             -------            ---         ---
      Disk       0               Online           447 GB        0 B                 *
      Disk       1               Online           3726 GB      15 MB            *
      Disk       2               Online           3726 GB      1024 KB         *
      Disk       3               Online           1907 GB      0 B                  *
    
    In this example, The system has a 500GB SSD in Disk 0. It has 2 4TB Hard drives at Disk 1 and Disk 2. It has a 2TB NVME on Disk 3. So if I wanted to install Windows on Disk 3, I would SELECT DISK 3 using the Disk Selection function (Option D) in the menu. The Default is Disk 0.
     
  14. fabricio simoes

    fabricio simoes MDL Senior Member

    Dec 17, 2020
    331
    90
    10

    I understand but I wanted my language but I have difficulty. I already tried to get it to translate but everything went wrong and I got discouraged. . .
     
  15. murphy78

    murphy78 MDL DISM Enthusiast

    Nov 18, 2012
    7,466
    11,872
    240
    Can I ask you why you are interested in using this script over the normal Microsoft Windows setup?
     
  16. fabricio simoes

    fabricio simoes MDL Senior Member

    Dec 17, 2020
    331
    90
    10
    Because I like DiskPart and image deployment...
     
  17. murphy78

    murphy78 MDL DISM Enthusiast

    Nov 18, 2012
    7,466
    11,872
    240
    I can help you do the commands without a script, but you need to learn a bit on how to look for help with them on your own.
    So this assumes that you have booted into a USB or DVD with an install.wim if you have an install.esd, swap the install.wim lines for install.esd
    Rem lines are just comment lines
    Also note that if you turn this into a script, you have to handle diskpart differently.
    Press Shift-F10 from any Microsoft Windows setup to bring up command prompt then:
    Code:
    Diskpart
    list disk
    select disk 0
    rem (or 3 or whatever disk you want to install to)
    clean
    convert gpt
    create partition efi size=200
    format quick fs=fat32 label="System"
    assign letter="S"
    create partition msr size=128
    create partition primary
    format quick fs=ntfs label="Windows"
    assign Letter="Z"
    exit
    
    rem (Following drive letter for install.wim could be different than D:, try different letters if D: doesn't work. Use same letter on both commands)
    dism /get-wiminfo /wimfile:D:\sources\install.wim
    
    rem (Look for the index number that you want to apply here)
    dism /apply-image /imagefile:D:\sources\install.wim /index:1 /applydir:Z:\
    
    bcdboot Z:\Windows
    
    wpeutil reboot
    
    This is a super basic set of commands you can use to set up your system using diskpart and dism. It doesn't set up an extra recovery partition.
    The default recovery option is that the windows partition is the same as the recovery partition unless you set up a different recovery partition.
    So if you use the above commands, it will have a recovery option enabled on everything except windows 7 because windows 7 doesn't enable recovery partitions by default.
    Also, if you wanted to copy the contents of an $OEM$ folder, you would need to do that manually.
     
  18. fabricio simoes

    fabricio simoes MDL Senior Member

    Dec 17, 2020
    331
    90
    10
    upload_2026-5-18_23-11-11.png
     
  19. murphy78

    murphy78 MDL DISM Enthusiast

    Nov 18, 2012
    7,466
    11,872
    240
    @fabricio simoes
    For install.swm files you have to use an extra /swmfile command and a install*.swm mask line to cover the install1.swm, install2.swm, etc
    Code:
    rem Instead of the original line
    dism /apply-image /imagefile:D:\sources\install.wim /index:1 /applydir:Z:\
    
    rem You add this extra part with the install*.swm
    dism /apply-image /imagefile:D:\sources\install.swm /swmfile:D:\sources\install*.swm /index:1 /applydir:Z:\
    
     
  20. fabricio simoes

    fabricio simoes MDL Senior Member

    Dec 17, 2020
    331
    90
    10
    #360 fabricio simoes, May 19, 2026
    Last edited: May 19, 2026
    what command to copy the $OEM$ folder?
    and apply along with the windows image?
    Winre is creating within the Windows Partition, is it possible to create a partition for the Windows recovery environment? How to do it?