Guide
Download Email Attachments in PowerShell
Download and process email attachments from Gmail, Outlook, Exchange, or any provider using Nylas CLI in PowerShell. One CLI, any provider, native PowerShell objects. No IMAP config, no provider-specific APIs.
Prerequisites
# Install Nylas CLI (one-liner for PowerShell)
irm https://cli.nylas.com/install.ps1 | iex
# Or install with Go (requires Go 1.21+)
# go install github.com/nylas/cli/cmd/nylas@latest
# Authenticate (one-time setup, opens browser)
nylas auth login
# Verify access
nylas email list --limit 1 --json | ConvertFrom-JsonList emails that have attachments
The --has-attachment flag filters to only messages with at least one attachment. Combined with --json, you get structured output you can pipe through PowerShell cmdlets.
# List recent emails with attachments
nylas email list --has-attachment --json | ConvertFrom-Json
# Limit results and see just subject and sender
nylas email list --has-attachment --json --limit 10 | `
ConvertFrom-Json | `
Select-Object id, subject, @{N='from';E={$_.from.email}}Get attachment IDs from a message
To download an attachment, you need its attachment ID and the message ID. Read a specific message to see its attachments:
# Read a message to see its attachments
$msg = nylas email read <message-id> --json | ConvertFrom-Json
# List all attachments on this message
$msg.attachments | Format-Table id, filename, content_type, size
# Example output:
# id filename content_type size
# -- -------- ------------ ----
# att_abc123 report.pdf application/pdf 245760
# att_def456 photo.jpg image/jpeg 1048576
# att_ghi789 data.csv text/csv 8192Download a single attachment
The nylas email attachments download command takes the attachment ID first, then the message ID:
# Download an attachment (attachment-id comes FIRST, then message-id)
nylas email attachments download att_abc123 msg_xyz789
# The file saves to the current directory with its original filename
# Output: Saved report.pdf (245760 bytes)Download all attachments from a message
Loop through every attachment on a message using ForEach-Object:
# Read the message and get attachments
$msg = nylas email read <message-id> --json | ConvertFrom-Json
# Download every attachment from this message
$msg.attachments | ForEach-Object {
Write-Host "Downloading $($_.filename)..."
nylas email attachments download $_.id $msg.id
}Save attachments to a specific directory
Create a target directory if it does not exist, then change into it before downloading:
# Create output directory
$outputDir = "C:\Users\$env:USERNAME\Downloads\attachments"
if (-not (Test-Path $outputDir)) {
New-Item -ItemType Directory -Path $outputDir | Out-Null
}
# Download all attachments from a message into that directory
$msg = nylas email read <message-id> --json | ConvertFrom-Json
Push-Location $outputDir
$msg.attachments | ForEach-Object {
nylas email attachments download $_.id $msg.id
}
Pop-Location
Write-Host "Saved $($msg.attachments.Count) files to $outputDir"Filter attachments by file type
Use Where-Object to download only specific file types:
# Download only PDF attachments from a message
$msg = nylas email read <message-id> --json | ConvertFrom-Json
$msg.attachments | `
Where-Object { $_.filename -like "*.pdf" } | `
ForEach-Object {
Write-Host "Downloading PDF: $($_.filename)"
nylas email attachments download $_.id $msg.id
}
# Download only images (jpg, png, gif)
$msg.attachments | `
Where-Object { $_.filename -match ".(jpg|jpeg|png|gif)$" } | `
ForEach-Object {
nylas email attachments download $_.id $msg.id
}
# Download only spreadsheets
$msg.attachments | `
Where-Object { $_.filename -match ".(xlsx|xls|csv)$" } | `
ForEach-Object {
nylas email attachments download $_.id $msg.id
}Bulk download from multiple messages
Combine --has-attachment with a loop to process attachments across many emails:
# Download all PDF attachments from the last 20 emails
$messages = nylas email list --has-attachment --json --limit 20 | `
ConvertFrom-Json
$outputDir = "C:\Users\$env:USERNAME\Downloads\bulk-attachments"
if (-not (Test-Path $outputDir)) {
New-Item -ItemType Directory -Path $outputDir | Out-Null
}
Push-Location $outputDir
$totalFiles = 0
$messages | ForEach-Object {
$detail = nylas email read $_.id --json | ConvertFrom-Json
$detail.attachments | `
Where-Object { $_.filename -like "*.pdf" } | `
ForEach-Object {
Write-Host " Downloading $($_.filename) from: $($detail.subject)"
nylas email attachments download $_.id $detail.id
$totalFiles++
}
}
Pop-Location
Write-Host "Downloaded $totalFiles PDF files to $outputDir"Build an attachment processing pipeline
PowerShell pipelines let you chain filtering, downloading, and post-processing into a single workflow:
# Pipeline: find invoices, download them, log results
$results = nylas email list --has-attachment --json --limit 50 | `
ConvertFrom-Json | `
ForEach-Object {
$detail = nylas email read $_.id --json | ConvertFrom-Json
$detail.attachments | `
Where-Object { $_.filename -match "invoice.*.pdf$" } | `
ForEach-Object {
nylas email attachments download $_.id $detail.id
[PSCustomObject]@{
MessageId = $detail.id
Subject = $detail.subject
From = $detail.from.email
Filename = $_.filename
Size = $_.size
Downloaded = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
}
}
}
# Display results as a table
$results | Format-Table -AutoSize
# Export to CSV for record-keeping
$results | Export-Csv -Path "invoice-downloads.csv" -NoTypeInformationSearch emails then download attachments
Combine nylas email search with attachment downloads to target specific emails:
# Search for emails about quarterly reports, download their attachments
$messages = nylas email search "quarterly report" --json --limit 10 | `
ConvertFrom-Json
$messages | ForEach-Object {
$detail = nylas email read $_.id --json | ConvertFrom-Json
if ($detail.attachments.Count -gt 0) {
Write-Host "Message: $($detail.subject)"
$detail.attachments | ForEach-Object {
Write-Host " -> $($_.filename)"
nylas email attachments download $_.id $detail.id
}
}
}Summarize attachments without downloading
Get a quick overview of all attachments in your recent emails:
# Summarize attachments from the last 30 emails with attachments
nylas email list --has-attachment --json --limit 30 | `
ConvertFrom-Json | `
ForEach-Object {
$detail = nylas email read $_.id --json | ConvertFrom-Json
$detail.attachments | ForEach-Object {
[PSCustomObject]@{
Subject = $detail.subject.Substring(0, [Math]::Min(40, $detail.subject.Length))
Filename = $_.filename
Type = $_.content_type
SizeKB = [Math]::Round($_.size / 1024, 1)
}
}
} | Format-Table -AutoSize
# Group by file extension to see what types you receive most
nylas email list --has-attachment --json --limit 50 | `
ConvertFrom-Json | `
ForEach-Object {
$detail = nylas email read $_.id --json | ConvertFrom-Json
$detail.attachments
} | `
ForEach-Object {
[IO.Path]::GetExtension($_.filename).ToLower()
} | `
Group-Object | `
Sort-Object Count -Descending | `
Format-Table Name, CountFrequently asked questions
How do I download email attachments from the command line in PowerShell?
Use Nylas CLI: first list emails with attachments using nylas email list --has-attachment --json, then read the message to get attachment IDs with nylas email read <id> --json | ConvertFrom-Json, and download with nylas email attachments download <attachment-id> <message-id>. The attachment ID comes first in the command.
Can I filter email attachments by file type in PowerShell?
Yes. After reading a message, pipe the attachments array through Where-Object. For example: $msg.attachments | Where-Object { $_.filename -like "*.pdf" } returns only PDF attachments. You can use -match with regex for multiple extensions.
Does Nylas CLI work with PowerShell on Windows?
Yes. Nylas CLI is a Go binary that runs natively on Windows. It works in PowerShell 5.1, PowerShell 7+, Command Prompt, and Windows Terminal. All --json output can be parsed with ConvertFrom-Json instead of jq.
How do I bulk download attachments from multiple emails?
List emails with nylas email list --has-attachment --json --limit 50, pipe through ConvertFrom-Json, then loop with ForEach-Object. For each message, read its details to get attachment metadata, filter if needed with Where-Object, and download each attachment.
Next steps
- Automate email and calendar in PowerShell -- combine email and calendar workflows
- Send email from the terminal -- compose and send emails from the CLI
- Secure email handling from the CLI -- safe attachment processing and phishing defense
- Full command reference -- every flag and subcommand