Disk to Image - Advanced Disk Imaging Utility
disk_to_image is a powerful and flexible Windows PowerShell utility that creates binary images of physical disks, like SD Cards. It is designed to be both user-friendly for interactive use and automation-ready for batch operations.
The script identifies mounted volumes on the disk to be copied, automatically dismounts all volumes to ensure no writes can occur during copying, also avoiding verification errors. At the end of the operation, it automatically remounts all volumes, restoring disk to original mounted state.
Nothing to install. Just copy the program and run it on Windows.
Tested on SD Cards.
- Full Disk Imaging: Create complete disk images or select specific partitions
- Smart SD Card Imaging: Create optimized images by copying only used space from SD cards
- Automatic End Detection: Automatically detects and stops at the last used partition
- Space Optimization: Avoids copying unused space beyond the last partition, resulting in smaller image files
- Flexible Buffer Management: Adjustable buffer size for optimized performance
- Data Verification: Built-in verification process to ensure data integrity
- No Installation Required: Standalone script that runs directly from PowerShell
- Interactive and Batch Modes: Run with command-line parameters or interactive prompts
- Partition-Level Control: Select specific partitions to image
- Robust Error Handling: Retry mechanisms and detailed error reporting
- Administrative Rights Management: Automatic elevation of privileges if not run as Administrator
To download and run the utility directly from GitHub using a Windows CMD:
# Download the script
powershell Invoke-WebRequest -Uri 'https://raw.githubusercontent.com/Ircama/disk_to_image/main/disk_to_image.ps1' -OutFile 'disk_to_image.ps1'
# Run interactively
powershell .\disk_to_image.ps1
# Or run with parameters
powershell .\disk_to_image.ps1 -DiskNumber 2 -Destination "D:\backups\disk2.bin"
-
Interactive Mode
Simply run the script without parameters:
.\disk_to_image.ps1
.The script will:
- Prompt for administrative rights if needed
- Ask whether to use partitions
- Guide you through the imaging process
-
Command-Line Mode
For automated or scripted operations:
.\disk_to_image.ps1 -DiskNumber 3 -Destination "D:\backups\disk3.bin" -UsePartitions -FirstPartition 0 -LastPartition 4 -Force
The utility is particularly valuable for backing up SD cards by:
- Reading all data from the start of the disk
- Automatically detecting the last used partition
- Creating an image that includes only the meaningful data
- Skipping unused space after the last partition
This approach offers several benefits:
- Smaller backup files
- Faster backup process
- Reduced storage requirements
- Perfect for embedded systems and IoT devices
Example for SD card backup:
.\disk_to_image.ps1 -DiskNumber 2 -Destination "D:\sdcard_backup.bin" -UsePartitions -FirstPartition 0 -LastPartition 4
When used with partitions (either interactively or via command-line arguments), the script will:
- Analyze the partition structure
- Copy from the beginning of the disk if 0 is used as the first partition
- Include all partitions if the last partition number is used
- Stop after the last partition, avoiding to copy unused space.
This is particularly useful for:
- Backing up Raspberry Pi SD cards
- Cloning embedded system storage
- Creating distributable images
- Efficient storage management.
Other useful features:
-
Verification Options:
- Built-in verification (default)
- Skip verification with -NoVerify
- Only verify existing images with -OnlyVerify
- Custom verification retry count with -RetryVerify
-
Performance Tuning:
- Adjustable buffer size (-BufferSize)
- Custom sector size specification (-SectorSize)
- Optimized read operations
-
Partition Management:
- Full disk or selected partitions
- Flexible partition range selection
- Zero-based offset support
Safety Features:
- Confirmation prompts (unless -Force is used)
- Error handling and retry mechanisms
- Progress reporting
- Debug information for verification issues
- Automatically dismounts disk before copying
- Safely remounts disk after completion
System Backup of the full disk:
.\disk_to_image.ps1 -DiskNumber 1 -Destination "D:\system_backup.bin"
Batch System Backup of the used part of the disk:
disk_to_image.ps1 -DiskNumber 2 -Destination k:\image.bin -UsePartitions -FirstPartition 0 -LastPartition 4 -Force
Quick batch System Backup of the used part of the disk without verification:
disk_to_image.ps1 -DiskNumber 2 -Destination "D:\quick_backup.bin" -UsePartitions -FirstPartition 0 -LastPartition 4 -Force -NoVerify
Specific Partition Backup:
.\disk_to_image.ps1 -DiskNumber 3 -Destination "D:\partition2.bin" -UsePartitions -FirstPartition 2 -LastPartition 2
Automated Backup:
.\disk_to_image.ps1 -DiskNumber 1 -Destination "D:\auto_backup.bin" -Force -BufferSize 10MB
Simple verification, without :
disk_to_image.ps1 -DiskNumber 2 -Destination k:\image.bin -UsePartitions -FirstPartition 0 -LastPartition 4 -Force -OnlyVerify
Usage: disk_to_image.ps1 -DiskNumber <string> -Destination <string> [options]
Description:
Copy disk to file image.
This script reads disk data writing an image file, allowing you to specify
partitions, buffer size, and verification options.
Needed Parameters:
-DiskNumber The number of the disk to process (e.g., "2").
-Destination The destination where data should be written (e.g.,
"C:\save\to\imagine.bin").
Optional Parameters:
-UsePartitions Use this switch to enable partition-specific operations.
-FirstPartition Specify the first partition to process (e.g., "1") or use
"0" for for disk start. Requires -UsePartitions.
-LastPartition Specify the last partition to process (e.g., "4").
Default is last partition. Requires -UsePartitions.
-BufferSize Specify the buffer size for the operation in MB
(default: 1MB).
Example: -BufferSize 10MB
-NoVerify Skip verification step (default is to verify)
-Force Do not ask any confirmation
-OnlyVerify Only perform the verification process, without copying
-OffsetVerify Start verification from given offset (
-DebugRetryVerify Write debug information when verify temporarily fails
-RetryVerify Number of verification retries of reading a sector
before showing error (default: 20)
-SectorSize Sector size in bytes (default is 512 bytes)
-help Show this help message.
Examples:
# Run the command interactively for full disk copy
.\disk_to_image.ps1
# Read Disk 2 and output to C:\save\to\imagine.bin with default settings:
.\disk_to_image.ps1 -DiskNumber 2 -Destination "C:\save\to\imagine.bin"
# Read Disk 2 with partitions 1 to 3 and a 10MB buffer:
.\disk_to_image.ps1 2 "C:\save\to\imagine.bin" -UsePartitions `
-FirstPartition 1 -LastPartition 3 -BufferSize 10MB
# Read Disk 2 without verification:
.\disk_to_image.ps1 2 "C:\save\to\imagine.bin" -NoVerify
For the inverse process, use VisualDiskImager.
To mount the partition(s) included in a disk image via WSL:
sudo losetup -fP --show /mnt/c/path/to/disk.img
lsblk /dev/loop0
Check the appropriate partition to mount (e.g., 1):
sudo mount /dev/loop0p1 /mnt