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
7:30 AM
Posts
5,182
Location
Texas, USA
OS
Win11 Pro 25H2 (RTM+)
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 My Computers

  • At a glance

    Win11 Pro 25H2 (RTM+)Intel i7-14650HX32 GBNo GPU - Built-in Intel Graphics
    OS
    Win11 Pro 25H2 (RTM+)
    Computer type
    PC/Desktop
    Manufacturer/Model
    Acemagic
    CPU
    Intel i7-14650HX
    Memory
    32 GB
    Graphics Card(s)
    No GPU - Built-in Intel Graphics
    Sound Card
    Integrated
    Monitor(s) Displays
    Varies as machine will often be moved to locations with different monitors
    Screen Resolution
    Varies
    Hard Drives
    1 x 1TB Gen 4 NVMe SSD
    PSU
    120W Power Brick
    Keyboard
    Corsair K70 Max RGB Magnetic Keyboard
    Mouse
    Logitech MX Master 3
    Internet Speed
    1Gb Up / 1 Gb Down
    Browser
    Edge
    Antivirus
    Windows Defender
  • At a glance

    Win11 Pro 25H2 (RTM+)Intel i7-1255U16 GBIntel Iris Xe Graphics
    Operating System
    Win11 Pro 25H2 (RTM+)
    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 My Computer

At a glance

I'm off Windows at home.
OS
I'm off Windows at home.
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 My Computer

At a glance

Windows 7
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 My Computers

  • At a glance

    Win11 Pro 25H2 (RTM+)Intel i7-14650HX32 GBNo GPU - Built-in Intel Graphics
    OS
    Win11 Pro 25H2 (RTM+)
    Computer type
    PC/Desktop
    Manufacturer/Model
    Acemagic
    CPU
    Intel i7-14650HX
    Memory
    32 GB
    Graphics Card(s)
    No GPU - Built-in Intel Graphics
    Sound Card
    Integrated
    Monitor(s) Displays
    Varies as machine will often be moved to locations with different monitors
    Screen Resolution
    Varies
    Hard Drives
    1 x 1TB Gen 4 NVMe SSD
    PSU
    120W Power Brick
    Keyboard
    Corsair K70 Max RGB Magnetic Keyboard
    Mouse
    Logitech MX Master 3
    Internet Speed
    1Gb Up / 1 Gb Down
    Browser
    Edge
    Antivirus
    Windows Defender
  • At a glance

    Win11 Pro 25H2 (RTM+)Intel i7-1255U16 GBIntel Iris Xe Graphics
    Operating System
    Win11 Pro 25H2 (RTM+)
    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 My Computer

At a glance

Windows 7
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 My Computer

At a glance

Windows 11AMD Ryzen 7 5700GMicron Technology DDR4-3200 16GBNVIDIA GeForce RTX 3060
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 My Computers

  • At a glance

    Win11 Pro 25H2 (RTM+)Intel i7-14650HX32 GBNo GPU - Built-in Intel Graphics
    OS
    Win11 Pro 25H2 (RTM+)
    Computer type
    PC/Desktop
    Manufacturer/Model
    Acemagic
    CPU
    Intel i7-14650HX
    Memory
    32 GB
    Graphics Card(s)
    No GPU - Built-in Intel Graphics
    Sound Card
    Integrated
    Monitor(s) Displays
    Varies as machine will often be moved to locations with different monitors
    Screen Resolution
    Varies
    Hard Drives
    1 x 1TB Gen 4 NVMe SSD
    PSU
    120W Power Brick
    Keyboard
    Corsair K70 Max RGB Magnetic Keyboard
    Mouse
    Logitech MX Master 3
    Internet Speed
    1Gb Up / 1 Gb Down
    Browser
    Edge
    Antivirus
    Windows Defender
  • At a glance

    Win11 Pro 25H2 (RTM+)Intel i7-1255U16 GBIntel Iris Xe Graphics
    Operating System
    Win11 Pro 25H2 (RTM+)
    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
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

On a live system you could create a shadow copy and create a wim from it using dism. Could get the info from vssadmin list shadows

It is easier using wimlib for example :

wimlib-imagex.exe append %systemdrive% "N:\mybackup.wim" --snapshot --create
 

My Computers My Computers

  • At a glance

    Win7,Win11i7-97002x16gb 3600mhz
    OS
    Win7,Win11
    Computer type
    PC/Desktop
    CPU
    i7-9700
    Motherboard
    gigabyte b365m ds3h
    Memory
    2x16gb 3600mhz
    Monitor(s) Displays
    benq gw2480
    PSU
    bequiet pure power 11 400CM
    Cooling
    cryorig m9i
  • At a glance

    win7,win11i5-84002x8gb 3200
    Operating System
    win7,win11
    Computer type
    PC/Desktop
    CPU
    i5-8400
    Motherboard
    gigabyte b365m ds3h
    Memory
    2x8gb 3200
    PSU
    xfx pro 450
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.

bmrui.exe is in ms supplied boot.wim.

or if necessary it can be done with diskgenius see below link
 
Last edited:

My Computers My Computers

  • At a glance

    Win7,Win11i7-97002x16gb 3600mhz
    OS
    Win7,Win11
    Computer type
    PC/Desktop
    CPU
    i7-9700
    Motherboard
    gigabyte b365m ds3h
    Memory
    2x16gb 3600mhz
    Monitor(s) Displays
    benq gw2480
    PSU
    bequiet pure power 11 400CM
    Cooling
    cryorig m9i
  • At a glance

    win7,win11i5-84002x8gb 3200
    Operating System
    win7,win11
    Computer type
    PC/Desktop
    CPU
    i5-8400
    Motherboard
    gigabyte b365m ds3h
    Memory
    2x8gb 3200
    PSU
    xfx pro 450

Latest Support Threads

Back
Top Bottom