← script library

File Backup Script

Backup & RecoveryJanuary 11, 2025

In this post, we share a PowerShell script designed to automate the backup of files from a specified source directory to a target backup directory. This script is essential for maintaining data safety and ensuring that you have copies of important files.

powershell
$sourceDirectory = 'C:\ImportantFiles'
$backupDirectory = 'D:\Backup'

if (-Not (Test-Path -Path $backupDirectory)) {
    New-Item -ItemType Directory -Path $backupDirectory
    Write-Host "Created backup directory: $backupDirectory"
}

Get-ChildItem -Path $sourceDirectory -File | ForEach-Object {
    $targetFilePath = Join-Path -Path $backupDirectory -ChildPath $_.Name
    Copy-Item -Path $_.FullName -Destination $targetFilePath -Force
    Write-Host "Backed up: $($_.Name) to $backupDirectory"
}

Write-Host "Backup process completed. All files from '$sourceDirectory' have been backed up to '$backupDirectory'."

Run it across your fleet

This script runs as-is on a single host. Paste it into ServerEngine to schedule it, run it on a whole server group in parallel, and keep the credentials out of the file — see the scripts documentation and the credential store.

Ready when you are.

Try ServerEngine free for 7 days.