Windows Deployment sample scripts

The following scripts are used in Apply Windows images, Build to order: Use online servicing to make final updates to individual PCs, and Deploy Windows images: Activate and secure PCs to set up PCs by using a Windows image file, and to configure push-button reset features. They can be downloaded from the OEM Partner Center.

Most of these code samples are plain-text, and can be saved from Notepad. A few samples, where noted, must be saved in the UTF-8 file format.

Samples are provided for both UEFI-based and BIOS-based PCs.

These scripts are used in Apply Windows images, Build to order: Use online servicing to make final updates to individual PCs, and Deploy Windows images: Activate and secure PCs to set up PCs by using a Windows image file.

Use these scripts together with DiskPart to format and set up the hard disk partitions for Windows, including recovery tools. Adjust the partition sizes to fill the drive as necessary.

  • UEFI:

    Creates the Windows RE Tools partition, the System, MSR, Windows, and Recovery image partitions.

    This script temporarily assigns these drive letters: WinRE Tools = T, System=S, Windows=C, and Recovery=R. If you’re deploying to PCs with unformatted hard drives, change the Windows drive letter to a letter that’s near the end of the alphabet, such as W, to avoid drive letter conflicts. Do not use X, because this drive letter is reserved for Windows PE. After the PC reboots, the Windows partition is assigned the letter C, and the other partitions don’t receive drive letters.

    The following diagram shows the resulting partition configuration:

    Diagram of EFI partitions with recovery
    rem == CreatePartitions-UEFI.txt ==
    rem == These commands are used with DiskPart to
    rem    set up the drive and ecreate five 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. Windows RE tools partition ===============
    create partition primary size=300
    format quick fs=ntfs label="Windows RE tools"
    assign letter="T"
    set id="de94bba4-06d1-4d40-a16a-bfd50179d6ac"
    gpt attributes=0x8000000000000001
    rem == 2. 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 == 3. Microsoft Reserved (MSR) partition =======
    create partition msr size=128
    rem == 4. Windows partition ========================
    rem ==    a. Create the Windows partition ==========
    create partition primary 
    rem ==    b. Create space for the recovery image ===
    shrink minimum=15000
    rem ==    c. Prepare the Windows partition ========= 
    format quick fs=ntfs label="Windows"
    assign letter="C"
    rem === 5. Recovery image partition ================
    create partition primary
    format quick fs=ntfs label="Recovery image"
    assign letter="R"
    set id="de94bba4-06d1-4d40-a16a-bfd50179d6ac"
    gpt attributes=0x8000000000000001
    list volume
    exit
    
  • BIOS:

    Creates the System, Windows, and Recovery image partitions.

    This script temporarily assigns these drive letters: System=S, Windows=C, and Recovery=R. If you’re deploying to PCs with unformatted hard drives, change the Windows drive letter to a letter that’s near the end of the alphabet, such as W, to avoid drive letter conflicts. Do not use X, because this drive letter is reserved for Windows PE. After the PC reboots, the Windows partition is assigned the letter C, and the other partitions don’t receive drive letters.

    The following diagram shows the resulting partition configuration:

    Diagram of partitions: Recovery, System, Windows
    rem == CreatePartitions-BIOS.txt ==
    rem == These commands are used with DiskPart to
    rem    set up the drive and create three partitions
    rem    for a BIOS/MBR-based PC.
    rem    Adjust the partition sizes to fill the drive
    rem    as necessary. ==
    select disk 0
    clean
    rem == 1. System partition ======================
    create partition primary size=350
    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 image  
    shrink minimum=15000
    rem ==    c. Prepare the Windows partition ====== 
    format quick fs=ntfs label="Windows"
    assign letter="C"
    rem == 3. Recovery image partition ==============
    create partition primary
    format quick fs=ntfs label="Recovery image"
    assign letter="R" 
    set id=27
    list volume
    exit
    

Use this script after creating the hard disk partitions to apply a Windows image to the PC and to set up each of the partitions. Files from the Windows image are used to configure each of the newly-created partitions.

  • UEFI:

    rem == ApplyImage-UEFI.bat ==
    
    rem == These commands deploy a specified Windows
    rem    image file to the Windows partition, and configure
    rem    the system partition.
    
    rem    Usage:   ApplyImage WimFileName 
    rem    Example: ApplyImage E:\Images\ThinImage.wim ==
    
    rem == Copy the image to the recovery partition ==
    md R:\RecoveryImage
    copy %1 R:\RecoveryImage\Install.wim
    
    rem == Apply the image to the Windows partition ==
    dism /Apply-Image /ImageFile:"R:\RecoveryImage\Install.wim" /Index:1 /ApplyDir:C:\
    
    rem == Copy boot files to the System partition ==
    C:\Windows\System32\bcdboot C:\Windows /s S:
    
    :rem == Copy the Windows RE image to the
    :rem    Windows RE Tools partition ==
    md T:\Recovery\WindowsRE
    xcopy /h C:\Windows\System32\Recovery\Winre.wim T:\Recovery\WindowsRE\
    
    :rem == Register the location of the recovery tools ==
    C:\Windows\System32\Reagentc /Setreimage /Path T:\Recovery\WindowsRE /Target C:\Windows
    
    :rem == Register the location of the
    :rem    push-button reset recovery image. ==
    C:\Windows\System32\Reagentc /Setosimage /Path R:\RecoveryImage /Target C:\Windows /Index 1
    
    :rem == Restrict permissions of recovery folder to local Admin group
    C:\Windows\System32\icacls R:\RecoveryImage /inheritance:r /T
    C:\Windows\System32\icacls R:\RecoveryImage /grant:r SYSTEM:(F) /T
    C:\Windows\System32\icacls R:\RecoveryImage /grant:r *S-1-5-32-544:(F) /T
    
    :rem == Verify the configuration status of the images. ==
    C:\Windows\System32\Reagentc /Info /Target C:\Windows
    
  • BIOS:

    rem == ApplyImage-BIOS.bat ==
    
    rem == These commands deploy a specified Windows
    rem    image file to the Windows partition, and configure
    rem    the system partition.
    
    rem    Usage:   ApplyImage WimFileName 
    rem    Example: ApplyImage E:\Images\ThinImage.wim ==
    
    rem == Copy the image to the recovery partition ==
    md R:\RecoveryImage
    copy %1 R:\RecoveryImage\Install.wim
    
    rem == Apply the image to the Windows partition ==
    dism /Apply-Image /ImageFile:"R:\RecoveryImage\Install.wim" /Index:1 /ApplyDir:C:\
    
    rem == Copy boot files to the System partition ==
    C:\Windows\System32\bcdboot C:\Windows
    
    :rem == Copy the Windows RE image to the
    :rem    System partition ==
    md S:\Recovery\WindowsRE
    xcopy /h C:\Windows\System32\Recovery\Winre.wim S:\Recovery\WindowsRE\
    
    :rem == Register the location of the recovery tools ==
    C:\Windows\System32\Reagentc /Setreimage /Path S:\Recovery\WindowsRE /Target C:\Windows
    
    :rem == Register the location of the
    :rem    push-button reset recovery image. ==
    C:\Windows\System32\Reagentc /Setosimage /Path R:\RecoveryImage /Target C:\Windows /Index 1
    
    :rem == Restrict permissions of recovery folder to local Admin group
    C:\Windows\System32\icacls R:\RecoveryImage /inheritance:r /T
    C:\Windows\System32\icacls R:\RecoveryImage /grant:r SYSTEM:(F) /T
    C:\Windows\System32\icacls R:\RecoveryImage /grant:r *S-1-5-32-544:(F) /T
    
    :rem == Verify the configuration status of the images. ==
    C:\Windows\System32\Reagentc /Info /Target C:\Windows
    

Use these scripts with DiskPart to hide the recovery partitions after they have been configured.

  • UEFI:

    rem === HideRecoveryPartitions-UEFI.txt ===
    select disk 0
    select partition 1
    remove
    set id=de94bba4-06d1-4d40-a16a-bfd50179d6ac
    gpt attributes=0x8000000000000001
    select partition 5
    remove
    set id=ebd0a0a2-b9e5-4433-87c0-68b6b72699c7
    gpt attributes=0x0000000000000001
    list volume
    exit
    
    
  • BIOS:

    rem === HideRecoveryPartitions-BIOS.txt ===
    select disk 0
    select partition 3
    set id=27
    remove
    list volume
    exit
    

These configuration files are used in Apply Windows images and Deploy Windows images: Activate and secure PCs to help support bare-metal recovery.

Save the originals in the E:\Deployment\RecoveryImage-BIOS and E:\Deployment\RecoveryImage-UEFI folders where E is the drive letter of the external USB hard drive. These files will be copied to the recovery image partition folder: R:\RecoveryImage\.

Each of the code samples in this section must be saved in the UTF-8 file format.

ResetConfig.xml is a configuration file for the push-button reset feature. This file loads the other configuration files, and defines the partition configuration after you have configured a bare metal reset.

This XML file must be saved using the UTF-8 file format. For example, in Notepad, click File, and then click Save As. In the Encoding box, click UTF-8, and save this file. Do not use ANSI coding.

Each XML file must be named ResetConfig.xml. If you are deploying both UEFI and BIOS PCs, create separate folders to store each of these files.

  • UEFI:

    <?xml version="1.0" encoding="utf-8"?>
    <!-- ResetConfig.xml - UEFI version - UTF-8 File format-->
       <Reset>
          <Run Phase="BasicReset_BeforeImageApply">
             <Path>SaveLogFiles.cmd</Path>
             <Duration>4</Duration>
          </Run>      
          <Run Phase="BasicReset_AfterImageApply">
             <Path>RetrieveLogFiles.cmd</Path>
             <Duration>2</Duration>
          </Run>
          <Run Phase="FactoryReset_AfterDiskFormat">
             <Path>CheckPartitions.exe</Path>
             <Duration>2</Duration>
          </Run>
          <Run Phase="FactoryReset_AfterImageApply">
             <Path>InstallApps.cmd</Path>
             <Param>/allApps</Param>
             <Duration>2</Duration>
          </Run>
        <SystemDisk>
            <DiskpartScriptPath>ResetPartitions-UEFI.txt</DiskpartScriptPath>
            <MinSize>75000</MinSize>
            <WindowsREPartition>1</WindowsREPartition>
            <WindowsREPath>Recovery\WindowsRE</WindowsREPath>
            <OSPartition>4</OSPartition>
            <RecoveryImagePartition>5</RecoveryImagePartition>
            <RecoveryImagePath>RecoveryImage</RecoveryImagePath>
            <RestoreFromIndex>1</RestoreFromIndex>
            <RecoveryImageIndex>1</RecoveryImageIndex>
        </SystemDisk>
       </Reset>
    
  • BIOS:

    <?xml version="1.0" encoding="utf-8"?>
    <!-- ResetConfig.xml - BIOS version - UTF-8 File format-->
       <Reset>
          <Run Phase="BasicReset_BeforeImageApply">
             <Path>SaveLogFiles.cmd</Path>
             <Duration>4</Duration>
          </Run>      
          <Run Phase="BasicReset_AfterImageApply">
             <Path>RetrieveLogFiles.cmd</Path>
             <Duration>2</Duration>
          </Run>
          <Run Phase="FactoryReset_AfterDiskFormat">
             <Path>RunDxDiag.cmd</Path>
             <Duration>2</Duration>
          </Run>
          <Run Phase="FactoryReset_AfterImageApply">
             <Path>RunDxDiag.cmd</Path>
             <Duration>2</Duration>
          </Run>
        <SystemDisk>
            <DiskpartScriptPath>ResetPartitions-BIOS.txt</DiskpartScriptPath>
            <MinSize>75000</MinSize>
            <WindowsREPartition>1</WindowsREPartition>
            <WindowsREPath>Recovery\WindowsRE</WindowsREPath>
            <OSPartition>2</OSPartition>
            <RecoveryImagePartition>3</RecoveryImagePartition>
            <RecoveryImagePath>RecoveryImage</RecoveryImagePath>
            <RestoreFromIndex>1</RestoreFromIndex>
            <RecoveryImageIndex>1</RecoveryImageIndex>
        </SystemDisk>
       </Reset>
    

This script shows how to add actions to the push-button reset feature. It is run when a user refreshes the PC, and it is referenced in ResetConfig.xml.

This script must be saved by using the UTF-8 file format. For example, in Notepad, click File, and then click Save As. In the Encoding box, click UTF-8, and save this file. Do not use ANSI coding.

:rem === SaveLogFiles.cmd - UTF-8 File format ===

:rem == This sample script preserves files that would 
:rem    otherwise be removed by placing them in a 
:rem    temporary location in memory, to be retrieved by
:rem    RetrieveLogFiles.cmd.

:rem == 1. Use the registry to identify the location of
:rem       the new operating system and the primary hard
:rem       drive. For example, 
:rem       %TARGETOS% may be defined as C:\Windows
:rem       %TARGETOSDRIVE% may be defined as C:
for /F "tokens=1,2,3 delims= " %%A in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\RecoveryEnvironment" /v TargetOS') DO SET TARGETOS=%%C

for /F "tokens=1 delims=\" %%A in ('Echo %TARGETOS%') DO SET TARGETOSDRIVE=%%A

:rem == 2. Copy old logs to a temporary folder in memory
mkdir X:\Temp
xcopy %TARGETOS%\Logs\*.* X:\temp /cherkyi

EXIT 0

This script shows how to add actions to the push-button reset feature. It is run when a user refreshes the PC, and it is referenced in ResetConfig.xml.

This script must be saved by using the UTF-8 file format. For example, in Notepad, click File, and then click Save As. In the Encoding box, click UTF-8, and save this file. Do not use ANSI coding.

:rem === RetrieveLogFiles.cmd - UTF-8 File format ===

:rem == This sample script retrieves the files that 
:rem    were saved in memory by 
:rem    SaveLogFiles.cmd,
:rem    and adds them back to the system.
:rem

:rem == 1. Use the registry to identify the location of
:rem       the new operating system and the primary hard
:rem       drive. For example, 
:rem       %TARGETOS% may be defined as C:\Windows
:rem       %TARGETOSDRIVE% may be defined as C:
for /F "tokens=1,2,3 delims= " %%A in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\RecoveryEnvironment" /v TargetOS') DO SET TARGETOS=%%C

for /F "tokens=1 delims=\" %%A in ('Echo %TARGETOS%') DO SET TARGETOSDRIVE=%%A

:rem == 2. Copy the old logs to the new OS 
:rem       at C:\Windows\OldLogs
mkdir %TARGETOS%\OldLogs
xcopy X:\Temp\*.* %TARGETOS%\OldLogs /cherkyi

EXIT 0

This script shows how to add actions to the push-button reset feature. It is run when a user resets the PC, and it is referenced in ResetConfig.xml.

This script must be saved using the UTF-8 file format. For example, in Notepad, click File, and then click Save As. In the Encoding box, click UTF-8, and save this file. Do not use ANSI coding.

:rem === RunDxDiag.cmd - UTF-8 File format ===

:rem == This sample script runs a system diagnostic,
:rem    and sends the output to the C:\Fabrikam folder.


:rem == 1. Use the registry to identify the location of
:rem       the new operating system and the primary hard
:rem       drive. For example, 
:rem       %TARGETOS% may be defined as C:\Windows
:rem       %TARGETOSDRIVE% may be defined as C:
for /F "tokens=1,2,3 delims= " %%A in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\RecoveryEnvironment" /v TargetOS') DO SET TARGETOS=%%C

:rem == 2. Run system diagnostics using the
:rem       DirectX Diagnostic tool, and save the 
:rem       results to the C:\Fabrikam folder. ==

mkdir %TARGETOSDRIVE%\Fabrikam
%TARGETOS%\system32\dxdiag.exe /whql:off /t %TARGETOSDRIVE%\Fabrikam\DxDiag-TestLogFiles.txt

EXIT 0

These scripts are used by push-button reset bare-metal recovery tools to repartition the hard drive after the hard drive has been reset.

  1. UEFI:

    rem === ResetPartitions-UEFI.txt ===
    rem == These commands are used with DiskPart to
    rem    reset the drive and recreate five partitions
    rem    for a UEFI/GPT-based PC.
    rem    Adjust the partition sizes to fill the drive
    rem    as necessary. ==
    rem == The differences between this file and
    rem    CreatePartitions-UEFI.txt
    rem    are noted in parenthesis.
    rem       (NOT USED: select disk 0)
    rem       (NOT USED: clean)
    convert gpt
    rem == 1. Windows RE tools partition ===============
    create partition primary size=300
    format quick fs=ntfs label="Windows RE tools"
    assign letter="T"
    set id="de94bba4-06d1-4d40-a16a-bfd50179d6ac"
    gpt attributes=0x8000000000000001
    rem == 2. 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 == 3. Microsoft Reserved (MSR) partition =======
    create partition msr size=128
    rem == 4. Windows partition ========================
    rem ==    a. Create the Windows partition ==========
    create partition primary 
    rem ==    b. Create space for the recovery image ===
    shrink minimum=15000
    rem ==    c. Prepare the Windows partition ========= 
    format quick fs=ntfs label="Windows"
    assign letter="C"
    rem === 5. Recovery image partition ================
    create partition primary
    format quick fs=ntfs label="Recovery image"
    assign letter="R"
    set id="de94bba4-06d1-4d40-a16a-bfd50179d6ac"
    gpt attributes=0x8000000000000001
    list volume
    exit
    
  2. BIOS:

    rem === ResetPartitions-BIOS.txt ===
    rem == These commands are used with DiskPart to
    rem    reset the drive and create three partitions
    rem    for a BIOS/MBR-based PC.
    rem    Adjust the partition sizes to fill the drive
    rem    as necessary. ==
    rem == The differences between this file and
    rem    CreatePartitions-BIOS.txt
    rem    are noted in parenthesis.
    rem       (NOT USED: select disk 0 )
    rem       (NOT USED: clean )
    rem == 1. System partition ======================
    create partition primary size=350
    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 image  
    shrink minimum=15000
    rem ==    c. Prepare the Windows partition ====== 
    format quick fs=ntfs label="Windows"
    assign letter="C"
    rem == 3. Recovery image partition ==============
    create partition primary
    format quick fs=ntfs label="Recovery image"
    assign letter="R" 
    set id=27
    list volume
    exit
    

This script creates a system report by using the DirectX Diagnostic tool, and sends it to a new folder called C:\Fabrikam.

md C:\Fabrikam
C:\Windows\System32\dxdiag /t C:\Fabrikam\DxDiag-TestLogFiles.txt

See Also

Show:
© 2014 Microsoft