PowerShell • SharePoint Online • Troubleshooting

PowerShell Email Works Manually but Fails in Task Scheduler

A real-world troubleshooting journey involving SharePoint Online, SMTP, Windows Task Scheduler, service accounts, and a deceptively successful PowerShell execution.

The short version

The PowerShell script worked perfectly when executed manually, but email delivery failed when the same script was launched through Windows Task Scheduler.

The SharePoint report itself was not the problem. The key difference was the Windows account used to execute the scheduled task.

The Problem

I was working with a PowerShell automation that generated a SharePoint Online site consumption report and emailed the resulting CSV file.

When I ran the script manually from PowerShell 7, everything worked. The SharePoint inventory was collected, the CSV was generated, and the email arrived successfully.

However, when the same script was executed through Windows Task Scheduler, something strange happened.

SCREENSHOT PLACEHOLDER #1 Add a screenshot of the failing Task Scheduler task showing the account used to run the task. Suggested filename: task-system-account.png

First: Was the SharePoint Report Actually Completing?

Before troubleshooting SMTP, I needed to determine whether the SharePoint portion of the script was actually completing.

The scheduled execution successfully retrieved more than 32,000 SharePoint sites and generated the final CSV report.

Connecting to SharePoint Admin Center...
Connected successfully.

Retrieving SharePoint site inventory...

Total Sites Found: 32611

Processing SharePoint sites...
Throttle Limit    : 3

Results Count = 32611

Exporting final CSV...

CSV created:
Mattel-SPOSiteConsumption_20260925_070755.csv

The report summary was also generated successfully.

Total Sites          : 32611
Storage Used         : 526.07 TB
Allocated Storage    : 159.51 TB
Over Utilized        : 366.56 TB
Over Utilized %      : 229.80%

This was important because it eliminated SharePoint connectivity and report generation as the primary cause.

Testing the Email Function Independently

The next step was to remove SharePoint completely from the equation.

I used the already-generated CSV and manually tested the same sender, SMTP server, recipient, and attachment.

The email arrived successfully.

What this test proved
  • SMTP connectivity was working.
  • The sender address was accepted.
  • The distribution list could receive email.
  • The attachment was not the problem.
  • The generated CSV was valid.

A Recipient Formatting Mistake

During troubleshooting, I temporarily added an individual email address alongside the distribution list.

Initially, I used this format:

$To = @(
    "messaging@example.com,user@example.com"
)

This looks reasonable at first glance, but it actually creates one array element containing a comma-separated string.

PowerShell then produced:

An invalid character was found in the mail header: ','.

The correct format is to provide each recipient as a separate array element:

$To = @(
    "messaging@example.com"
    "user@example.com"
)

This was a useful finding, but it still wasn't the underlying Task Scheduler delivery problem.

SCREENSHOT PLACEHOLDER #2 Add a screenshot showing the mail-header error and, if useful, the incorrect recipient configuration. Suggested filename: smtp-error.png

The Important Clue: Execution Context

At this point, I compared how the script was being executed manually versus how Windows Task Scheduler was executing it.

This revealed a significant difference.

Environment Execution account Result
Manual PowerShell Administrator/user account Email delivered
Task Scheduler SYSTEM Email not delivered
Working scheduled task Dedicated service account Email delivered

The working automation on the same server was configured to use a dedicated service account rather than SYSTEM.

SCREENSHOT PLACEHOLDER #3 Add the Task Scheduler screenshot showing the working task configured with the service account. Suggested filename: working-service-account.png

Why does the execution account matter?

A PowerShell process launched interactively by an administrator does not necessarily have the same execution context as a process launched by Task Scheduler under SYSTEM.

The execution identity can affect:

Important

Do not assume that because a PowerShell script works under your administrator account, it will behave identically when executed by SYSTEM or a service account.

The Controlled Task Scheduler Test

Rather than repeatedly running a SharePoint inventory containing tens of thousands of sites, I created a very small PowerShell script that only sent an email.

$From = "report@example.com"
$To = "messaging@example.com"
$SmtpServer = "smtp.example.com"

$Body = @"
Task Scheduler SMTP Test

Computer : $env:COMPUTERNAME
User     : $([System.Security.Principal.WindowsIdentity]::GetCurrent().Name)
Time     : $(Get-Date)

This is a controlled SMTP test.
"@

Send-MailMessage `
    -From $From `
    -To $To `
    -Subject "Task Scheduler SMTP Test" `
    -Body $Body `
    -SmtpServer $SmtpServer `
    -ErrorAction Stop

I then configured a temporary Task Scheduler task to run this small test using the same PowerShell version as the production automation, but under the dedicated service account.

The test email arrived successfully.

This was the breakthrough

The isolated test demonstrated that Task Scheduler itself could send the email successfully when the task ran under the appropriate service-account context.

SCREENSHOT PLACEHOLDER #4 Add a screenshot of the controlled SMTP test task, preferably showing the service account and PowerShell 7. Suggested filename: task-scheduler-smtp-test.png
SCREENSHOT PLACEHOLDER #5 Add a screenshot of the successfully received test email. Suggested filename: smtp-test-success.png

Resolution

The production SharePoint reporting task was changed from the SYSTEM account to the appropriate dedicated service account.

The PowerShell 7 action and the SharePoint reporting script itself were left unchanged.

The recipient configuration remained simple:

$To = @(
    "messaging@example.com"
)

After the execution account was corrected, the scheduled automation was able to submit the email successfully and the distribution list received the report.

Lessons Learned

1. Task Scheduler success does not mean email delivery success

A Task Scheduler result of 0x0 indicates that the scheduled task completed successfully. It should not be treated as proof that an email reached the recipient's mailbox.

2. "Email sent successfully" is not end-to-end delivery confirmation

A successful Send-MailMessage call indicates that the SMTP operation completed from the PowerShell process's perspective. Downstream mail-flow processing can still affect final delivery.

3. Test SMTP separately from SharePoint

If a SharePoint report takes hours to generate, don't repeatedly execute the entire report just to test email delivery.

A small SMTP test script can isolate the email path in seconds.

4. Always compare execution identities

When a script behaves differently between manual execution and Task Scheduler, check the identity actually running the process.

[System.Security.Principal.WindowsIdentity]::GetCurrent().Name

5. Don't overlook the Task Scheduler Action

The account isn't the only thing that can differ. Also compare:

Troubleshooting Checklist

  1. Run the PowerShell script manually.
  2. Confirm that the SharePoint operation completes.
  3. Confirm that the expected CSV/report is generated.
  4. Send the generated attachment manually.
  5. Test SMTP without involving SharePoint.
  6. Check the Task Scheduler execution account.
  7. Compare the PowerShell version used by manual and scheduled execution.
  8. Create a minimal SMTP test under the exact Task Scheduler account.
  9. Test the distribution list separately from the full automation.
  10. Only after identifying the difference should you modify the production task.

Final Takeaway

When a PowerShell script works perfectly when run manually but behaves differently under Windows Task Scheduler, don't troubleshoot only the script.

Troubleshoot the execution environment as well.

In this case, SharePoint Online, CSV generation, SMTP connectivity and the email code were all functioning. The important difference was the account under which the scheduled task was running.

A small, isolated SMTP test made it possible to identify the difference without repeatedly running the complete SharePoint inventory.

M365Buddy Community

Discuss this article

Have a question, correction, or a helpful tip? Join the discussion. Posting and replying uses your GitHub account; please keep tenant details and credentials out of public comments.

GitHub comments are being connected

The embedded discussion will appear here after GitHub Discussions is enabled for M365Buddy and the comments category is configured.

Visit the M365Buddy Community → Report an issue on GitHub →