Guide

Send Email from PowerShell

PowerShell deprecated Send-MailMessage in version 7.0, and configuring raw SMTP from scripts means hardcoding credentials. Nylas CLI is a single Go binary that handles OAuth2, provider abstraction, and connection management. Install it, authenticate once, and send email from PowerShell with one command. Works with Gmail, Outlook, Exchange, Yahoo, iCloud, and IMAP.

Written by Qasim Muhammad Staff SRE

Reviewed by Nick Barraclough

VerifiedCLI 3.1.1 · Gmail, Outlook · last tested April 11, 2026

Why not raw SMTP from PowerShell?

PowerShell's Send-MailMessage was deprecated in PowerShell 7.0 because it cannot negotiate TLS securely. The alternatives -- System.Net.Mail.SmtpClient and direct socket connections -- require you to hardcode SMTP credentials, manage app passwords, and handle provider-specific quirks. Gmail requires OAuth2 or app passwords with 2FA. Outlook requires an app registration in Azure AD. Every provider is different.

Nylas CLI abstracts all of this. One binary, one authentication flow, every provider.

1. Install Nylas CLI on Windows

Nylas CLI is a single Go binary. Install it with one command:

# One-liner install for PowerShell
irm https://cli.nylas.com/install.ps1 | iex

# Or install with Go (requires Go 1.23+)
# go install github.com/nylas/cli/cmd/nylas@latest

Verify the installation:

nylas --version

2. Authenticate your mailbox

Head to dashboard-v3.nylas.com, create an application, and connect your mailbox. Then configure the CLI:

# Configure your API key
nylas auth config
# Paste your API key when prompted

# Verify it works
nylas auth whoami
# => Authenticated as you@company.com (Google Workspace)

3. Build commands with PowerShell-native argument arrays

This is where the PowerShell guide should differ from the generic terminal page. On Windows, the main friction is quoting, escaping, and reusing arguments across scripts. PowerShell arrays make that cleaner than long line continuations.

$sendArgs = @(
  "email", "send",
  "--to", "colleague@company.com",
  "--cc", "manager@company.com",
  "--subject", "Quarterly report",
  "--body", "Hi -- please review the Q4 numbers before Friday.",
  "--yes"
)

& nylas @sendArgs

4. HTML bodies, attachments, and file-backed content

PowerShell is strong at loading files, building strings, and handing paths around as variables. Use that instead of cramming everything into one long command:

$htmlBody = Get-Content -Path ".\email-template.html" -Raw
$attachments = @(
  "C:\Users\you\Documents\invoice-march.pdf",
  "C:\Users\you\Documents\terms.pdf"
)

$args = @(
  "email", "send",
  "--to", "client@example.com",
  "--subject", "Invoice attached",
  "--body", $htmlBody,
  "--attach", $attachments[0],
  "--attach", $attachments[1],
  "--yes"
)

& nylas @args

5. Send from PowerShell objects and CSV data

Another Windows-native pattern is turning CSV rows or object properties directly into email sends. That is more useful for admins than the generic Unix examples because the input data usually already lives in a CSV export, scheduled report, or AD-derived object.

Import-Csv -Path ".\contacts.csv" | ForEach-Object {
  $body = @"
Hello $($_.Name),

Your account is ready.
"@

  $args = @(
    "email", "send",
    "--to", $_.Email,
    "--subject", "Hello $($_.Name)",
    "--body", $body,
    "--yes"
  )

  & nylas @args
}

6. Schedule from Task Scheduler and scheduled jobs

The Windows deployment model matters here. If a script runs unattended, test it the way Windows runs it: from Task Scheduler, under the intended service account, with an explicit working directory and no interactive prompt.

$action = New-ScheduledTaskAction -Execute "pwsh.exe" -Argument "-NoProfile -File C:\Scripts\Send-DailyReport.ps1"
$trigger = New-ScheduledTaskTrigger -Daily -At 8:00AM

Register-ScheduledTask -TaskName "Daily Email Report" -Action $action -Trigger $trigger
# Or schedule delivery directly from the CLI
$scheduledArgs = @(
  "email", "send",
  "--to", "team@company.com",
  "--subject", "Standup agenda",
  "--body", "Please add your items.",
  "--schedule", "tomorrow 9am",
  "--yes"
)

& nylas @scheduledArgs

7. JSON output and PowerShell objects

Every Nylas CLI command supports --json output. Use PowerShell's ConvertFrom-Json to parse it:

# Count unread emails
$unread = nylas email list --unread --json | ConvertFrom-Json
Write-Host "Unread emails: $($unread.Count)"

# Get subjects of unread emails
$unread | ForEach-Object { Write-Host $_.subject }

# Find urgent emails
$urgent = nylas email list --unread --json |
  ConvertFrom-Json |
  Where-Object { $_.subject -match "urgent|asap" }
Write-Host "Urgent emails: $($urgent.Count)"

# Send results to a file
nylas email list --limit 50 --json |
  ConvertFrom-Json |
  Select-Object subject, from, date |
  Export-Csv -Path ".inbox-report.csv" -NoTypeInformation

8. PowerShell-native error handling

PowerShell also rewards a different scripting style than bash. Use try/catch, inspect $LASTEXITCODE, and log structured output that can be consumed by scheduled jobs or monitoring tools. That is a very different operational pattern from the Unix pipeline examples in the terminal guide.

try {
  $args = @(
    "email", "send",
    "--to", "alerts@company.com",
    "--subject", "Windows service restart",
    "--body", "The Print Spooler service was restarted on HOST-17.",
    "--yes"
  )

  & nylas @args

  if ($LASTEXITCODE -ne 0) {
    throw "nylas email send failed with exit code $LASTEXITCODE"
  }
}
catch {
  Write-Error $_
  Exit 1
}

Frequently asked questions

Can I send email from PowerShell without Send-MailMessage?

Yes. Install Nylas CLI, authenticate once, and use nylas email send. It handles OAuth2 and TLS negotiation automatically -- no SMTP credentials, no deprecated cmdlets.

Does Nylas CLI work in 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 commands and flags are identical across shells.

How do I send HTML email from PowerShell?

Pass HTML in the --body flag or read it from a file with Get-Content -Raw. The CLI detects HTML content automatically and sets the correct MIME type.

Can I schedule emails from PowerShell?

Yes. Add --schedule "2h" or --schedule "tomorrow 9am" to any send command. Supports relative times and absolute dates.

Next steps