Ben Traje
← Back to python

Batch Compress Files into Standalone Split Archives with PowerShell

25 Jul 26 (2mo ago)

Okay, not a Python script, but I often find myself zipping (or RAR-ing) a bunch of files into parts. That is, rather than sharing a single 1GB archive, splitting it into five 200MB parts.

Normally, you can directly do that in standard archive software UI and it splits everything into volumes (.part1.rar, .part2.rar, etc.).

The problem with traditional multi-part archives is that you need to download every single part before you can open the contents.

I wanted a way to split files across separate, standalone ZIP archives automatically—where each ZIP is completely independent. For convenience and flexibility, here is a simple PowerShell script (.ps1) to do just that.

PowerShell Script

Save this code as Split-Zip.ps1:

param(
    [string]$SourceDir = ".",
    [long]$MaxSizeBytes = 200MB  # Change to your desired limit (e.g., 50MB, 100MB, 500MB)
)

$files = Get-ChildItem -Path $SourceDir -File$partNumber = 1
$currentSize = 0$currentGroup = @()

foreach ($file in$files) {
    # Check if adding the next file exceeds the target size
    if (($currentSize +$file.Length) -gt $MaxSizeBytes -and$currentGroup.Count -gt 0) {
        $zipName = "Part_$partNumber.zip"
        Compress-Archive -Path $currentGroup.FullName -DestinationPath$zipName -Force
        Write-Host "Created $zipName ($([math]::Round($currentSize / 1MB, 2)) MB)"

        $partNumber++
        $currentGroup = @()$currentSize = 0
    }

    # Handle edge case: a single file that is larger than $MaxSizeBytes on its own
    if ($file.Length -gt$MaxSizeBytes) {
        $zipName = "Part_$partNumber.zip"
        Compress-Archive -Path $file.FullName -DestinationPath$zipName -Force
        Write-Host "Created $zipName (Single large file: $([math]::Round($file.Length / 1MB, 2)) MB)"
        $partNumber++
        continue
    }

    $currentGroup +=$file
    $currentSize +=$file.Length
}

# Compress any remaining files in the final group
if ($currentGroup.Count -gt 0) {
    $zipName = "Part_$partNumber.zip"
    Compress-Archive -Path $currentGroup.FullName -DestinationPath$zipName -Force
    Write-Host "Created $zipName ($([math]::Round($currentSize / 1MB, 2)) MB)"
}

How to Use It

  1. Drop Split-Zip.ps1 right into the folder containing the files you want to zip.
  2. Open PowerShell in that folder and run:
.\Split-Zip.ps1

That’s it! It will group your files and generate standalone Part_1.zip, Part_2.zip, etc., right inside the folder.

(Optional: If you ever need a size limit other than 200MB, you can pass it in the terminal like .\Split-Zip.ps1 -MaxSizeBytes 500MB)