This article will guide you through the process of creating a USB flash drive specifically for loading operating system (OS) images using the Deployment Image Servicing and Management (DISM) tool and PowerShell. The USB drive created will allow you to run PowerShell scripts at boot time, providing a convenient and flexible solution for OS deployment.
Prerequisites
To begin, gather the following items and software:
- A USB flash drive with a capacity of at least 8GB
- A Windows system with PowerShell 5.0 or later
- The Windows Assessment and Deployment Kit (ADK) for your target OS version
Preparing the USB Flash Drive
Start by formatting the USB flash drive with the NTFS file system. This can be done using the Disk Management tool in Windows or by using the following PowerShell script:
Get-WmiObject -Class Win32_Volume -Filter "DriveType=1 AND DriveLetter="X:"" |
Format-Volume -FileSystem NTFS -AllocationUnitSize 4096 -Confirm:$false -Force
Replace "X:" with the correct drive letter for your USB flash drive.
Creating a Bootable USB Drive
Next, create a bootable USB drive using the DISM tool and the Windows Preinstallation Environment (WinPE) from the Windows ADK. Follow these steps:
- Mount the WinPE ISO file to a folder on your system. For example:
Mount-WindowsImage -ImagePath "C:\WinPE_amd64.iso" -Path "C:\Mount"
- Copy the contents of the mounted folder (C:\Mount\WinPE\_AMD64) to the root of the USB drive.
- Create a boot entry for the USB drive using bcdboot:
bcdboot "C:\Mount\WinPE_AMD64" -d "Windows PE" -s X:
Replace "X:" with the correct drive letter for your USB flash drive.
Running PowerShell Scripts at Boot Time
To run PowerShell scripts at boot time, follow these steps:
- Create a new folder on the USB drive, for example "Boot".
- Inside the new folder, create a new text file named "custom.cmd" with the following content:
@echo off
powershell.exe -ExecutionPolicy Bypass -File "X:\Scripts\BootScript.ps1"
Replace "X:\Scripts\BootScript.ps1" with the path to your boot script on the USB drive.
- Add the "custom.cmd" file to the WinPE boot entry. Edit the BCD store on the USB drive using bcdedit:
bcdedit /store X:\boot\bcd /set {bootmgr} Path \EFI\Microsoft\Boot\custom.efi
Replace "X:" with the correct drive letter for your USB flash drive.
- Formatted the USB flash drive with the NTFS file system
- Created a bootable USB drive using DISM and WinPE from the Windows ADK
- Configured the USB drive to run a PowerShell boot script using a custom.cmd file and the BCD store