PowerShell: Download file & copy it

You can use the script below download a file from a certain location, to a temporary file, do a small check, and then copy it to a location. It also sends an email with some feedback.

# Define variables
$sourceUrl = "https://example.com/yourfile.ext"    # Replace with the URL of the file to download
$localPath = "C:\LocalFolder\yourfile.ext"        # Local file path
$remotePath = "\\Server\Share\yourfile.ext"        # Remote file path
$backupPath = "\\Server\Share\Backups"            # Remote backup folder path
$smtpServer = "smtp.example.com"                   # Your SMTP server
$senderEmail = "[email protected]"                # Your email address
$recipientEmail = "[email protected]"          # Recipient's email address
$emailSubject = "File Download and Transfer Report"
$emailBody = ""

# Function to download the file from the internet
Function Download-File {
    param (
        [string]$url,
        [string]$localPath
    )
    try {
        $webClient = New-Object System.Net.WebClient
        $webClient.DownloadFile($url, $localPath)
        return $true
    } catch {
        Write-Host "Error downloading the file: $_"
        return $false
    }
}

# Function to check if the local file is valid
Function Validate-File {
    param (
        [string]$localPath
    )
    # Add your validation logic here, e.g., check the file size or format
    if (Test-Path $localPath -and (Get-Item $localPath).Length -gt 0) {
        return $true
    } else {
        Write-Host "Local file is invalid or doesn't exist."
        return $false
    }
}

# Function to create a backup of the remote file
Function Create-Remote-Backup {
    param (
        [string]$remotePath,
        [string]$backupPath
    )
    $timestamp = Get-Date -Format "yyyyMMddHHmmss"
    $backupFileName = Join-Path $backupPath ("backup_$timestamp.yourfile.ext")
    Copy-Item -Path $remotePath -Destination $backupFileName -Force
}

# Download the file
$downloaded = Download-File -url $sourceUrl -localPath $localPath

if ($downloaded) {
    # Validate the local file
    $isValid = Validate-File -localPath $localPath

    if ($isValid) {
        # Create a backup of the remote file
        Create-Remote-Backup -remotePath $remotePath -backupPath $backupPath

        # Copy the file to the remote location
        Copy-Item -Path $localPath -Destination $remotePath -Force

        # Get file details
        $fileDetails = Get-Item $localPath
        $fileSize = $fileDetails.Length
        $fileName = $fileDetails.Name
        $fileDateTime = $fileDetails.LastWriteTime

        # Prepare email body with file details
        $emailBody = "File downloaded successfully and copied to the remote location.
File Details:
Name: $fileName
Size: $fileSize bytes
DateTime: $fileDateTime"
    } else {
        $emailBody = "File download was successful, but the local file is invalid."
    }
} else {
    $emailBody = "File download failed."
}

# Send an email with the details
Send-MailMessage -WarningAction:SilentlyContinue -SmtpServer $smtpServer -From $senderEmail -To $recipientEmail -Subject $emailSubject -Body $emailBody
Share your love