Dans cet article, nous allons voir comment automatiser la sauvegarde d’un dossier sur une clé USB dès son insertion, sans installer de logiciel tiers. Nous utiliserons PowerShell pour la copie et la compression, ainsi qu’un fichier batch pour lancer le script facilement.
Préparation : Le Script PowerShell
- Copier un dossier vers la clé USB
- Afficher une barre de progression
- Compresser la sauvegarde en ZIP
- Afficher une notification Windows à la fin
Le Script PowerShell
Crée un fichier backup.ps1 sur ta clé USB et colle ce code :
La ligne a modifier pour sélectionner le dossier a copier est : $source = « C:\Users\infos perso »
#Detect the USB drive where the script is running
$usbDrive = $PSScriptRoot.Substring(0,3)
Write-Host "The script is executed from : $usbDrive" -ForegroundColor Green
# Define source and destination paths
$source = "C:\Users\infos perso" # Change to the folder you want to backup
$destination = "$usbDrive\BackupUSB"
$zipFile = "$usbDrive\BackupUSB_$(Get-Date -Format 'yyyy-MM-dd').zip"
$logFile = "$destination\backup_log.txt"
# Check if source folder exists
if (!(Test-Path -Path $source)) {
Write-Host "Error: Source folder does not exist!" -ForegroundColor Red
exit
}
# Create destination folder if it doesn't exist
if (!(Test-Path -Path $destination)) {
New-Item -ItemType Directory -Path $destination | Out-Null
}
# List files to copy
$files = Get-ChildItem -Path $source -Recurse -ErrorAction SilentlyContinue
$totalFiles = $files.Count
$counter = 0
# Copy files with progress bar
foreach ($file in $files) {
if (![string]::IsNullOrEmpty($file.FullName)) {
$destinationPath = "$destination\" + ($file.FullName -replace [regex]::Escape($source), "")
$destinationDir = Split-Path -Path $destinationPath -Parent
if (![string]::IsNullOrEmpty($destinationDir)) {
if (!(Test-Path -Path $destinationDir)) {
New-Item -ItemType Directory -Path $destinationDir -Force | Out-Null
}
Copy-Item -Path $file.FullName -Destination $destinationPath -Force -ErrorAction SilentlyContinue
} else {
Write-Host "⚠️ Error: Empty destination folder for $file.FullName" -ForegroundColor Red
}
} else {
Write-Host "⚠️ Error: Empty source path!" -ForegroundColor Red
}
}
#Compression in ZIP using Windows (Compress-Archive)
Write-Host "Compression in progress..."
if (Test-Path -Path $zipFile) {
Remove-Item -Path $zipFile -Force
}
Compress-Archive -Path "$destination\*" -DestinationPath $zipFile -Force
Write-Host "Compression completed: $zipFile"
#Backup completed
Write-Host "Backup Completed!" | Out-File -Append -FilePath $logFile
#Display Windows Notification
[System.Windows.Forms.MessageBox]::Show("Backup completed successfully!", "Backup USB", 0, [System.Windows.Forms.MessageBoxIcon]::Information)
Exécution Automatique avec un Fichier .bat
Pour exécuter ce script sans ligne de commande, crée un fichier start_backup.bat avec le bloc note et mets ce code dedans et enregistrer le fichier en .bat exemple start_backup.bat :
@echo off
powershell -ExecutionPolicy Bypass -File "%~d0\backup.ps1"
exitDouble-clique sur start_backup.bat pour lancer la sauvegarde !
Grâce à PowerShell et un simple fichier .bat, tu peux automatiser la sauvegarde de tes fichiers sur une clé USB sans installer de logiciel tiers. Cette méthode est gratuite, rapide et efficace.
Tu peux aussi adapter ce script pour sauvegarder d’autres dossiers ou ajouter un envoi vers un cloud.
