2026-07-15

Powershell EMail: How to add new line in email Body?

Stefan Bogdanescu

Stefan Bogdanescu

Founder & Senior Architect

Powershell EMail: How to add new line in email Body?

Powershell Email: How to Add New Lines in the Email Body – A Developer's Guide

As developers working with automation scripts, especially those involving external protocols like SMTP for email delivery, we often run into subtle but frustrating issues related to character encoding and formatting. You’ve written a script that seems logically correct, yet the output doesn't match the expectation—in this case, missing line breaks in the email body.

This post dives deep into why standard newline characters (\n) or HTML tags (<br>) fail when sending emails via PowerShell’s Send method, and provides robust solutions for ensuring your messages are properly formatted.

The Mystery of Missing Newlines in Email Bodies

When you construct an email body using simple string concatenation in PowerShell, the formatting often breaks down because email systems rely on specific MIME (Multipurpose Internet Mail Extensions) standards to interpret line breaks and content types.

You attempted several common solutions: %0d%0a, \n, and <br>. While these work perfectly fine within standard HTML documents or console output, they often fail when passed directly through a basic SMTP client interface, as the mail server expects content to be strictly structured according to its protocol rules.

The reason your attempts failed is likely due to the content type of the email you are sending. If the body is treated as plain text (text/plain), line breaks are sometimes collapsed or ignored by default unless explicitly handled by the encoding system. If the body is intended to be HTML, the server needs to process the markup correctly.

The Developer Solution: Mastering HTML Formatting

The most reliable way to ensure multi-line content appears correctly in an email is to treat the message as HTML rather than plain text. This allows you to use standard HTML tags for formatting, which are universally understood by mail clients.

If your goal is to add a line break within an HTML email, you must use the appropriate HTML tag for line breaks, or ensure you are concatenating the body properly into an HTML structure.

Correct Approach using HTML Tags

For adding a visible line break in an HTML context, the standard method is using the <br> tag, but this should be wrapped correctly within the overall message structure. Since your initial attempt with <br> failed, it suggests the issue might be how the entire $Body string is being interpreted by the SMTP client versus how it’s rendered by the recipient.

A more robust method involves ensuring that every required line break is explicitly defined as an HTML element:

$EmailFrom = "monitoring@mydomainname.no"
$EmailTo = "fatherazrael@tcs.com"
$Subject = "Disk Space Low: $server"

# Construct the body using explicit HTML line breaks for clarity
$Body = @"
<html>
<body>
    <p>Server Name: $server</p>
    <br>
    <p>Drive: C</p>
    <br>
    <p>Total Size: $sizeGB</p>
    <br>
    <p>Space Left: $freeSpaceGB</p>
</body>
</html>
"@

$SMTPServer = "scan.opinergo.fn"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25)
# ... rest of the sending code
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)

Notice how we wrapped the entire content in <html><body>...</body></html>. This explicitly tells the email client that the content is formatted HTML, making the <br> tags valid directives for line breaks within the message structure. This approach provides far greater control than simple character insertion.

Best Practices and System Design

When designing systems that interact with external services, like sending emails, it’s crucial to treat data as structured objects rather than raw strings. Just as in building scalable application logic, structuring your data cleanly prevents these kinds of formatting errors downstream. When architecting solutions, think about how different systems (PowerShell, SMTP protocol, and the final email client) interpret the same data format. This principle applies whether you are dealing with backend scripting or modern framework design, much like principles found in robust architectures seen in platforms such as Laravel.

Conclusion

The issue of line breaks in PowerShell emails is rarely about a missing character; it’s almost always about content encoding and structure. Stop relying on raw characters like \n or %0a for complex formatting. Instead, adopt the HTML approach: wrap your entire message within standard HTML tags and use explicit HTML elements like <br> to manage line breaks. This ensures that your email is correctly interpreted by virtually all mail clients, delivering a clean and professional result every time.

Note: Blog content is currently available in English.

Tags:

Enhance your marketing setup with your own email marketing platform.

Join the growing number of SaaS platforms using Laravel Mail to offer email marketing solutions to their customers.