0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
JPPINTO
  • Home
  • Blog
  • Certifications
  • About
  • Contact
  • Shop
  • Gallery
  • Current Setup
Contact

Search

June 24, 2026 / PowerShell, Productivity, Windows 10

Reduce PDF File Size with Ghostscript and PowerShell

Tags: document management, file compression, ghostscript, pdf, powershell, windows
Featured image for 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 .pdf file.
  • Skips files that already include -compressed in the filename.
  • Creates a new file beside the original.
  • Keeps the original PDF untouched.
  • Adds -compressed to 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 -compressed in the name are skipped so the script does not keep recompressing its own output.

References

  • Ghostscript 10.07.1 release on GitHub
  • Ghostscript downloads
Post Views: 17
<- Do Markdown Files Save Claude Tokens? A Practical Guide to Claude Context and Cost Control

Categories

  • Active Directory (5)
  • AI (2)
  • Amazon Cloud Services (1)
  • Blazor (1)
  • C# (C-Sharp) (3)
  • CI/CD Pipelines (1)
  • Containers (4)
  • Deployment (2)
  • Development (4)
  • Docker (3)
  • General (5)
  • IIS 6.0 (4)
  • IIS 7.0 (10)
  • IIS 8.0 (1)
  • Infrastructure as Code (IaC) (1)
  • Kubernetes (3)
  • Microsoft 365 (2)
  • MySQL (1)
  • Office 2010 (1)
  • PHP (1)
  • PowerShell (7)
  • Productivity (1)
  • SharePoint 2007 (8)
  • SharePoint 2010 (19)
  • SharePoint 2013 (2)
  • SharePoint Online (1)
  • SMTP (4)
  • SQL Server 2008 (1)
  • SQL Server 2008 R2 (1)
  • SQL Server 2012 (2)
  • SQL Server 2019 (1)
  • Uncategorized (1)
  • URL Rewrite (2)
  • Visual Studio 2019 (1)
  • Visual Studio Code (1)
  • Windows 10 (5)
  • Windows 2003 (9)
  • Windows 2008 (18)
  • Windows 2012 (6)
  • Windows 7 (3)
  • Windows Firewall (1)
  • Windows Vista (1)
  • WordPress (3)
  • WP-CLI (3)

Recent Posts

  • Reduce PDF File Size with Ghostscript and PowerShell
  • Do Markdown Files Save Claude Tokens? A Practical Guide to Claude Context and Cost Control
  • Use AGENTS.md, CLAUDE.md, Cursor Rules, and Prompt Logs to Keep AI Coding Bots in Context
  • Copy WordPress Plugin Settings from Dev to Production with WP-CLI
  • Move WordPress Articles from Dev to Production with WP-CLI

Advertisement

Tags

ai coding agents backconnectionhostnames custom column developer workflow dev to production disable shutdown event tracker error opening exe exe permissions externalize blob externalize sharepoint data filezilla server firewall rules filazilla full installation http redirect https https redirect IIS iis7 iis 7 installation IIS installation index server configuration installing cumulative updates load balance central administration microsoft 365 moss advanced search nlb powerpoint powershell redirect http to https search column sharepoint 2010 cumulative updates sharepoint 2010 farm build sharepoint 2010 farm configuration sharepoint 2010 farm installation sharepoint data externalization SMTP storagepoint windows Windows 7 windows firewall configuration windows server 2008 wlbs wordpress wp-cli x86
© 2026 JPPinto.com – Tech Blog. All rights reserved.