Doing a clean install of Windows? Here's a batch file to make an image backup and driver backup first.


hsehestedt

Well-known member
Guru
VIP
Local time
9:11 AM
Posts
4,368
Location
Texas, USA
OS
Win11 Pro 24H2
Very often, we see discussions that include the advice to make a backup image of your system. Frequently, this advice is targeted to people who simply don't make regular backups and have no backup software.

For those individuals, I've created a little batch file that will use the command line WBADMIN to make an image backup. In addition, it will export all drivers from the system so that you have a separate backup of all drivers ready to be re-imported on a clean installation.

Before everyone hammers me on this: Yes, I know that the built-in disk imaging utility in Windows is horrible. Yes, I know it is deprecated. But it can still prove useful if it is all that you have.

One weird quirk of WBADMIN is that it creates the backup only in a folder named WindowsImageBackup in the root of the target drive and locks it down so that you have no access to the contents. So, after the backup is created this batch file will traverse that WindowsImageBackup folder giving the current user ownership of all files / folders.

To use, just copy the text below into a text file and name it something.bat (any name with a .bat extension is fine).


Batch:
:: *****************************
:: * Quick Backup              *
:: * by Hannes Sehestedt       *
:: * Last update June 20, 2025 *
:: *****************************

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: This batch file will make a backup copy of all drivers installed on your     ::
:: system and then it will use WBADMIN to make a disk image backup of your      ::
:: system (C:, Recovery partition, EFI Partition) to a drive that you will      ::
:: specify. WBADMIN will only backup to the root of a drive creating a folder   ::
:: by the name of WindowsImageBackup. Another annoyance is that sets            ::
:: permissions on that folder in a way that makes easy access difficult. So,    ::
:: to work around this, after the backup is created this batch file will give   ::
:: the currently logged on user ownership and permission to access this folder. ::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


@echo off
setlocal enabledelayedexpansion
setlocal enableextensions
cd /d %~dp0

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Check to see if this batch file is being run as Administrator. If it is not, then rerun the batch file ::
:: automatically as admin and terminate the intial instance of the batch file.                            ::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

(Fsutil Dirty Query %SystemDrive%>Nul)||(PowerShell start """%~f0""" -verb RunAs & Exit /B)

::::::::::::::::::::::::::::::::::::::::::::::::
:: End Routine to check if being run as Admin ::
::::::::::::::::::::::::::::::::::::::::::::::::


:: Ask for the drive letter to which backups should be saved.

echo Enter the drive letter to which we should backup the system and save drivers.
echo Please enter the letter only (no colon).
echo.
set /p Target="Enter the target drive letter (letter only): "

:: Create a folder named Drivers_Backup on the target drive
:: We will save a copy of all drivers to this location.

md %Target%:\Drivers_Backup
pnputil /export-driver * %Target%:\Drivers_Backup

:: Perform the disk image backup

wbadmin start backup -backuptarget:%Target%: -allcritical -quiet

:: Take ownership of the WindowsImageBackup folder on the target drive

set SourcePath=%Target%:\WindowsImageBackup
C:\windows\system32\takeown /f "%SourcePath%"
icacls "%SourcePath%" /grant %username%:F
C:\windows\system32\takeown /f "%SourcePath%\*.*" /r /d y
icacls "%SourcePath%\*.*" /T /grant %username%:F

cls
echo All operations have been completed.
echo.
pause
 

My Computers

System One System Two

  • OS
    Win11 Pro 24H2
    Computer type
    PC/Desktop
    Manufacturer/Model
    Self-built
    CPU
    Intel i7 11700K
    Motherboard
    ASUS Prime Z590-A MB
    Memory
    64GB (Waiting for warranty replacement of another 64GB for 128GB total)
    Graphics Card(s)
    No GPU - Built-in Intel Graphics
    Sound Card
    Integrated
    Monitor(s) Displays
    HP Envy 32
    Screen Resolution
    2560 x 1440
    Hard Drives
    1 x 1TB NVMe SSD
    1 x 2TB NVMe SSD
    1 x 4TB NVMe SSD
    3 x 512GB 2.5" SSD
    1 x 4TB 2.5" SSD
    5 x 8TB Seagate Barracuda HDD
    PSU
    Corsair HX850i
    Case
    Corsair iCUE RGB 5000X mid tower case
    Cooling
    Noctua NF-S12A chromax.black.swap case fans (Qty. 7) & Home Computer Specifications, Configuration, and Usage Notes General Specifications ASUS Prime Z590-A motherboard, serial number M1M0KC222467ARP Intel Core i7-11700K CPU (11th Gen Rocket Lake / LGA 1200 Socket) 128GB Crucial Ballistix RGB DDR4 3200 MHz DRAM (4 x 32GB) Corsair iCUE RGB 5000X mid tower case Noctua NH-D15 chromax.black CPU cooler Noctua NF-S12A chromax.black.swap case fans (Qty. 7) & Corsair LL-120 RGB Fans (Qty. 3)
    Keyboard
    Corsair K70 Max RGB Magnetic Keyboard
    Mouse
    Logitech MX Master 3
    Internet Speed
    1Gb Up / 1 Gb Down
    Browser
    Edge
    Antivirus
    Windows Defender
    Other Info
    The five 8TB drives and three 512GB SSDs are part of a DrivePool using StableBit DrivePool software. The three SSDs are devoted purely to caching for the 8TB drives. All of the important data is stored in triplicate so that I can withstand simultaneous failure of 2 disks.

    Networking: 2.5Gbps Ethernet and WiFi 6e
  • Operating System
    Win11 Pro 23H2
    Computer type
    Laptop
    Manufacturer/Model
    Lenovo ThinkBook 13x Gen 2
    CPU
    Intel i7-1255U
    Memory
    16 GB
    Graphics card(s)
    Intel Iris Xe Graphics
    Sound Card
    Realtek® ALC3306-CG codec
    Monitor(s) Displays
    13.3-inch IPS Display
    Screen Resolution
    WQXGA (2560 x 1600)
    Hard Drives
    2 TB 4 x 4 NVMe SSD
    PSU
    USB-C / Thunderbolt 4 Power / Charging
    Keyboard
    Backlit, spill resistant keyboard
    Mouse
    Buttonless Glass Precision Touchpad
    Internet Speed
    1Gb Up / 1Gb Down
    Browser
    Edge
    Antivirus
    Windows Defender
    Other Info
    WiFi 6e / Bluetooth 5.1 / Facial Recognition / Fingerprint Sensor / ToF (Time of Flight) Human Presence Sensor
But… but… it’s horrible and deprecated!! 😉
 

My Computer

System One

  • OS
    Windows 11 Pro 24H2 [rev. 4351]
    Computer type
    PC/Desktop
    Manufacturer/Model
    Intel NUC12WSHi7
    CPU
    12th Gen Intel Core i7-1260P, 2100 MHz
    Motherboard
    NUC12WSBi7
    Memory
    64 GB
    Graphics Card(s)
    Intel Iris Xe
    Sound Card
    built-in Realtek HD audio
    Monitor(s) Displays
    Dell U3219Q
    Screen Resolution
    3840x2160 @ 60Hz
    Hard Drives
    Samsung SSD 990 PRO 1TB
    Keyboard
    CODE 104-Key Mechanical with Cherry MX Clears
    Antivirus
    Microsoft Defender
Why not perform a bog standard WIM capture?

Unlike a Windows Image Backup, a WIM can be used at a future date to create a customized install image. Can't do that with a backup.
 

My Computer

System One

  • OS
    Windows 7
Why not perform a bog standard WIM capture?

Unlike a Windows Image Backup, a WIM can be used at a future date to create a customized install image. Can't do that with a backup.
Educate me - how would I do that on a live system? Do I simply run sysprep, boot from alternate media and then create an image much like with a system in audit mode or is there some other procedure to doing this?
 

My Computers

System One System Two

  • OS
    Win11 Pro 24H2
    Computer type
    PC/Desktop
    Manufacturer/Model
    Self-built
    CPU
    Intel i7 11700K
    Motherboard
    ASUS Prime Z590-A MB
    Memory
    64GB (Waiting for warranty replacement of another 64GB for 128GB total)
    Graphics Card(s)
    No GPU - Built-in Intel Graphics
    Sound Card
    Integrated
    Monitor(s) Displays
    HP Envy 32
    Screen Resolution
    2560 x 1440
    Hard Drives
    1 x 1TB NVMe SSD
    1 x 2TB NVMe SSD
    1 x 4TB NVMe SSD
    3 x 512GB 2.5" SSD
    1 x 4TB 2.5" SSD
    5 x 8TB Seagate Barracuda HDD
    PSU
    Corsair HX850i
    Case
    Corsair iCUE RGB 5000X mid tower case
    Cooling
    Noctua NF-S12A chromax.black.swap case fans (Qty. 7) & Home Computer Specifications, Configuration, and Usage Notes General Specifications ASUS Prime Z590-A motherboard, serial number M1M0KC222467ARP Intel Core i7-11700K CPU (11th Gen Rocket Lake / LGA 1200 Socket) 128GB Crucial Ballistix RGB DDR4 3200 MHz DRAM (4 x 32GB) Corsair iCUE RGB 5000X mid tower case Noctua NH-D15 chromax.black CPU cooler Noctua NF-S12A chromax.black.swap case fans (Qty. 7) & Corsair LL-120 RGB Fans (Qty. 3)
    Keyboard
    Corsair K70 Max RGB Magnetic Keyboard
    Mouse
    Logitech MX Master 3
    Internet Speed
    1Gb Up / 1 Gb Down
    Browser
    Edge
    Antivirus
    Windows Defender
    Other Info
    The five 8TB drives and three 512GB SSDs are part of a DrivePool using StableBit DrivePool software. The three SSDs are devoted purely to caching for the 8TB drives. All of the important data is stored in triplicate so that I can withstand simultaneous failure of 2 disks.

    Networking: 2.5Gbps Ethernet and WiFi 6e
  • Operating System
    Win11 Pro 23H2
    Computer type
    Laptop
    Manufacturer/Model
    Lenovo ThinkBook 13x Gen 2
    CPU
    Intel i7-1255U
    Memory
    16 GB
    Graphics card(s)
    Intel Iris Xe Graphics
    Sound Card
    Realtek® ALC3306-CG codec
    Monitor(s) Displays
    13.3-inch IPS Display
    Screen Resolution
    WQXGA (2560 x 1600)
    Hard Drives
    2 TB 4 x 4 NVMe SSD
    PSU
    USB-C / Thunderbolt 4 Power / Charging
    Keyboard
    Backlit, spill resistant keyboard
    Mouse
    Buttonless Glass Precision Touchpad
    Internet Speed
    1Gb Up / 1Gb Down
    Browser
    Edge
    Antivirus
    Windows Defender
    Other Info
    WiFi 6e / Bluetooth 5.1 / Facial Recognition / Fingerprint Sensor / ToF (Time of Flight) Human Presence Sensor
Specifically you mention exporting 3rd-party drivers. Which isn't needed for Windows Image Backup, but only used for a clean image install.

Maybe I'm wrong, but most of your previous work has involved scripts to help prepare install images. Windows Image Backup can be used as long as the Recovery environment is intact. But for cases where Recovery stopped working, you would be force to do a reinstall first before restoring from a Windows Image Backup save.

It would be useful to walk the user through a WinRE-based DISM /capture-image right after the initial Windows install, and use that to replace the default Windows install WIM. This way the user can install a pre-configured image which skips past the OOBE process, and can be used to jump to start a Windows Image Backup restore.
 

My Computer

System One

  • OS
    Windows 7
Code:
:: Backup and Restore Device Drivers in Windows 10 and Windows 11

@Echo Off & cls   
    
(Net session >nul 2>&1)||(PowerShell start """%~0""" -verb RunAs & Exit /B)   
    
Setlocal EnableDelayedExpansion
    
:choice

Cls & Mode CON  LINES=11 COLS=40 & Color 0E & Title Created By FreeBooter
Echo.
Echo.
Echo.
Echo        Export Drivers  (E)?   
Echo.       
Echo        Import Drivers  (I)?
Echo.


Set /p input=:^>

If /i  Not %input%==E (Goto :_Ex) Else (Goto :_Backup)

:_Ex
If /i Not %input%==I  (Goto :choice) Else (Goto :_Restore)


Cls & Mode CON  LINES=20 COLS=40 & Color 0E & Title Created By FreeBooter
:_Backup
    mode con cols=95 lines=5 & color 0E
    Call :BrowseForFolder "Please choose the source folder for to backup drivers" SourceFolder
    If defined SourceFolder (
        echo(
        echo             You chose "!SourceFolder!" as source folder
    ) else (
        echo(
        Color 0C & echo                    The source folder is not defined ... Exiting ......
        Timeout /T 2 /nobreak>nul & exit
    )
Cls & Mode CON  LINES=50 COLS=120 & Color 0E & Title Created By FreeBooter

Dism /online /export-driver /destination:"!SourceFolder!"

Timeout /T 10 /nobreak>nul & exit

    


:_Restore   
    mode con cols=95 lines=5 & color 0E
    
    Call :BrowseForFolder "Please choose the target folder for to restore drivers" TargetFolder
    If defined TargetFolder (
        echo(
        echo             You chose "!TargetFolder!" as Target folder
    ) else (
        echo(
        Color 0C & echo                    The Target folder is not defined ... Exiting ......
        Timeout /T 2 /nobreak>nul & exit
    )
Timeout /T 3 /nobreak>nul
    (
Cls & Mode CON  LINES=50 COLS=120 & Color 0E & Title Created By FreeBooter
    
pnputil /add-driver "!TargetFolder!\*.inf" /subdirs /install /reboot
    )
Timeout /T 10 /nobreak>nul & exit
    
    
    
    :BrowseForFolder
set "psCommand="(new-object -COM 'Shell.Application')^
.BrowseForFolder(0,'%1',0,0).self.path""
for /f "usebackq delims=" %%I in (`powershell %psCommand%`) do set "%2=%%I"
exit /b
 

My Computer

System One

  • OS
    Windows 11
    Computer type
    PC/Desktop
    Manufacturer/Model
    HP Pavilion
    CPU
    AMD Ryzen 7 5700G
    Motherboard
    Erica6
    Memory
    Micron Technology DDR4-3200 16GB
    Graphics Card(s)
    NVIDIA GeForce RTX 3060
    Sound Card
    Realtek ALC671
    Monitor(s) Displays
    Samsung SyncMaster U28E590
    Screen Resolution
    3840 x 2160
    Hard Drives
    SAMSUNG MZVLQ1T0HALB-000H1
you would be force to do a reinstall first before restoring from a Windows Image Backup save.
Yeah, my post could have been clearer, yet once again.

However, the way I have done the restores in testing is to simply boot from Windows installation media and chose the repair option. I could care less whether the recovery environment is working or even present on the system.

As for the use of the driver backup, this is a totally separate item. The backup would be in order to go back if something goes very wrong and the drivers are in case all goes well and the clean install is successful. In that case you will want to rapidly install your drivers that you backed up from the previous install.

I know that there are other ways. For example, I now do all my clean installs with an answer file to make them 100% touch free, and I use a $OEM$ to install all my drivers during Windows Setup and a $WinPeDriver$ for boot critical drivers. And in my case, I have Macrium Reflect backups in case it all goes wrong except on one laptop on which it is impossible to do restores with Reflect.
 

My Computers

System One System Two

  • OS
    Win11 Pro 24H2
    Computer type
    PC/Desktop
    Manufacturer/Model
    Self-built
    CPU
    Intel i7 11700K
    Motherboard
    ASUS Prime Z590-A MB
    Memory
    64GB (Waiting for warranty replacement of another 64GB for 128GB total)
    Graphics Card(s)
    No GPU - Built-in Intel Graphics
    Sound Card
    Integrated
    Monitor(s) Displays
    HP Envy 32
    Screen Resolution
    2560 x 1440
    Hard Drives
    1 x 1TB NVMe SSD
    1 x 2TB NVMe SSD
    1 x 4TB NVMe SSD
    3 x 512GB 2.5" SSD
    1 x 4TB 2.5" SSD
    5 x 8TB Seagate Barracuda HDD
    PSU
    Corsair HX850i
    Case
    Corsair iCUE RGB 5000X mid tower case
    Cooling
    Noctua NF-S12A chromax.black.swap case fans (Qty. 7) & Home Computer Specifications, Configuration, and Usage Notes General Specifications ASUS Prime Z590-A motherboard, serial number M1M0KC222467ARP Intel Core i7-11700K CPU (11th Gen Rocket Lake / LGA 1200 Socket) 128GB Crucial Ballistix RGB DDR4 3200 MHz DRAM (4 x 32GB) Corsair iCUE RGB 5000X mid tower case Noctua NH-D15 chromax.black CPU cooler Noctua NF-S12A chromax.black.swap case fans (Qty. 7) & Corsair LL-120 RGB Fans (Qty. 3)
    Keyboard
    Corsair K70 Max RGB Magnetic Keyboard
    Mouse
    Logitech MX Master 3
    Internet Speed
    1Gb Up / 1 Gb Down
    Browser
    Edge
    Antivirus
    Windows Defender
    Other Info
    The five 8TB drives and three 512GB SSDs are part of a DrivePool using StableBit DrivePool software. The three SSDs are devoted purely to caching for the 8TB drives. All of the important data is stored in triplicate so that I can withstand simultaneous failure of 2 disks.

    Networking: 2.5Gbps Ethernet and WiFi 6e
  • Operating System
    Win11 Pro 23H2
    Computer type
    Laptop
    Manufacturer/Model
    Lenovo ThinkBook 13x Gen 2
    CPU
    Intel i7-1255U
    Memory
    16 GB
    Graphics card(s)
    Intel Iris Xe Graphics
    Sound Card
    Realtek® ALC3306-CG codec
    Monitor(s) Displays
    13.3-inch IPS Display
    Screen Resolution
    WQXGA (2560 x 1600)
    Hard Drives
    2 TB 4 x 4 NVMe SSD
    PSU
    USB-C / Thunderbolt 4 Power / Charging
    Keyboard
    Backlit, spill resistant keyboard
    Mouse
    Buttonless Glass Precision Touchpad
    Internet Speed
    1Gb Up / 1Gb Down
    Browser
    Edge
    Antivirus
    Windows Defender
    Other Info
    WiFi 6e / Bluetooth 5.1 / Facial Recognition / Fingerprint Sensor / ToF (Time of Flight) Human Presence Sensor
Back
Top Bottom