Reduce PDF File Size with Ghostscript and PowerShell
Large PDF files are common when a document was scanned, exported with high-resolution images, or saved with settings that prioritize quality over file size. If you need to email a PDF, upload it to a portal, or archive several scanned files, reducing the file size can make the document easier to handle.
One reliable way to compress PDFs on Windows is to use Ghostscript with PowerShell. Ghostscript can rewrite a PDF with smaller image settings while keeping the file as a normal PDF.
This walkthrough installs Ghostscript 10.07.1, compresses every PDF in a folder, and then compares the before-and-after file sizes.
What It Does
The compression script:
- Looks in the same folder as the script.
- Finds every
.pdffile. - Skips files that already include
-compressedin the filename. - Creates a new file beside the original.
- Keeps the original PDF untouched.
- Adds
-compressedto the new file name. - Adds a number if a compressed file already exists.
For example:
example-document.pdf example-document-compressed.pdf example-document-compressed-1.pdf
When This Works Best
This works best for scanned PDFs, image-heavy PDFs, and documents where a smaller file is more important than preserving maximum image quality.
It may not reduce a PDF much if the file is already optimized, mostly text, or already contains compressed images.
Install Ghostscript
Ghostscript must be installed before the compression script can call gswin64c.
Save this as install-ghostscript.ps1:
Clear-Host
Push-Location $PSScriptRoot
Write-Host "Downloading Ghostscript 10.07.1 x64..."
$Installer = "$env:TEMP\gs10071w64.exe"
Invoke-WebRequest `
-Uri "https://github.com/ArtifexSoftware/ghostpdl-downloads/releases/download/gs10071/gs10071w64.exe" `
-OutFile $Installer
if (Test-Path $Installer) {
Write-Host "Installing Ghostscript..."
Start-Process -FilePath $Installer -ArgumentList "/S" -Wait
Write-Host "Checking gswin64c..."
$Gs = Get-ChildItem "C:\Program Files\gs" -Recurse -Filter "gswin64c.exe" -ErrorAction SilentlyContinue |
Select-Object -First 1
if ($Gs) {
Write-Host "Found: $($Gs.FullName)"
& $Gs.FullName --version
} else {
Write-Host "Install finished, but gswin64c.exe was not found."
Write-Host "Open a new PowerShell window and try: gswin64c --version"
}
} else {
Write-Host "Download failed."
}
After installation, open a new PowerShell window if gswin64c is not immediately found.
You can test Ghostscript with:
gswin64c --version
Check The Starting PDF Sizes
Before compressing files, check the current file sizes:
Get-ChildItem -File |
Select-Object Name, @{Name='SizeMB';Expression={[math]::Round($_.Length / 1MB, 2)}} |
Sort-Object SizeMB -Descending
Example before compression:
Name SizeMB ---- ------ 2026.05.28 scanned-document.pdf 6.25 2026.06.23 scanned-document.pdf 3.52
How to Run the Script
Put compress-pdfs-with-ghostscript.ps1 in the same folder as the PDF files you want to compress.
Generic command:
.\script_location\scriptname.ps1
Concrete example:
Set-Location C:\Scripts\PdfCompression .\compress-pdfs-with-ghostscript.ps1
If PowerShell blocks the script, see:
Make Sure PowerShell Scripts Can Run on Windows
PowerShell Script
Clear-Host
Push-Location $PSScriptRoot
Get-ChildItem -Path . -Filter *.pdf -File |
Where-Object { $_.BaseName -notlike "*-compressed*" } |
ForEach-Object {
Write-Host "Compressing $($_.Name)..."
$BaseOut = Join-Path $_.DirectoryName "$($_.BaseName)-compressed.pdf"
$Out = $BaseOut
$i = 1
while (Test-Path $Out) {
$Out = Join-Path $_.DirectoryName "$($_.BaseName)-compressed-$i.pdf"
$i++
}
$Args = @(
"-sDEVICE=pdfwrite",
"-dCompatibilityLevel=1.4",
"-dPDFSETTINGS=/ebook",
"-dNOPAUSE",
"-dQUIET",
"-dBATCH",
"-sOutputFile=$Out",
$_.FullName
)
& gswin64c @Args
if ($LASTEXITCODE -eq 0 -and (Test-Path $Out)) {
Write-Host "Created $Out"
} else {
Write-Host "Failed $($_.Name)"
}
}
Save this as compress-pdfs-with-ghostscript.ps1:
Check The Results
After the script finishes, run the size check again:
Get-ChildItem -File |
Select-Object Name, @{Name='SizeMB';Expression={[math]::Round($_.Length / 1MB, 2)}} |
Sort-Object SizeMB -Descending
Example after compression:
Name SizeMB ---- ------ 2026.05.28 scanned-document.pdf 6.25 2026.06.23 scanned-document.pdf 3.52 2026.05.28 scanned-document-compressed.pdf 2.39 2026.06.23 scanned-document-compressed.pdf 1.49
In this example, the first PDF went from 6.25 MB to 2.39 MB, and the second went from 3.52 MB to 1.49 MB.
Compression Settings
This script uses:
-dPDFSETTINGS=/ebook
That is a good middle-ground setting for many scanned documents. It usually makes files much smaller without making the PDF unreadable.
Other common Ghostscript settings include:
/screen: smaller files, lower quality./ebook: balanced size and readability./printer: larger files, higher quality./prepress: larger files, high-quality print use.
If the compressed PDF is too blurry, try /printer. If the file is still too large, try /screen.
Important Notes
- Always review the compressed PDF before deleting the original.
- Keep the original file until you confirm the compressed copy is readable.
- Compression results vary depending on how the PDF was created.
- This script does not overwrite the original PDF.
- Files with
-compressedin the name are skipped so the script does not keep recompressing its own output.