Sending Email From PowerShell via Exchange Server
Easily Email Your Daily Reports Through PowerShell Script

Table of Contents
Sending Email from PowerShell by Using Exchange Server 2019
If you want to know about sending email from PowerShell, you have landed in the correct place.
Many admins and users set the pre-defined messages that they have to write again and again.
Instead of writing the message again and again, they save it as a template and make some minor changes, and then send the email.
Same way, if you have to send some sort of daily reports for the user’s activity.
The quickest way to send the message is a PowerShell.
You can save the predefined messages, make necessary changes, and paste them into the PowerShell to send the email.
However, you can save the code as a script and run the same script to send the message daily.
Even with the help of PowerShell, you can send an email with an Attachment as well.
So, in this article, we will learn how to send a simple email via PowerShell.
Therefore, we will also see how we can generate csv report and send an email along with an attachment.
How to Send Email with PowerShell without an Attachment
Before sending an email through PowerShell, make sure that you have an email service running in your organization, such as Exchange Server or any other Email Platform that supports the function.
Here, in our case, we have Microsoft Exchange 2019 running in the organization.
So, with the help of Exchange, we will send the email with PowerShell.
However, we need a simple code to send an email without an attachment.
Format the below code according you’re your details
$sendMailMessageSplat = @{
From = 'Administrator <administrator@techijack.net>'
To = 'Jack <jack@techijack.net>'
Subject = 'Test mail'
Body = 'Pls find the user daily report'
smtpServer = 'mail.techijack.net'
}
Send-MailMessage @sendMailMessageSplat
You can change “From, To, Subject, Body, and SmtpServer”
Make sure you put your valid email address and the SMTP Server
Once the above code is executed, we have received an email in the user Jack’s inbox as below
So, we have successfully sent an email with PowerShell.
How to Send Email from PowerShell With an Attachment
Sending an email with an attachment is a bit tricky.
Here we will use the script to first create a reports folder inside the C drive
Therefore, we will generate the CSV report for all users’ last logon activity by the name of the report and the current date.
Finally, we will attach the generated report in the email and send it to the user, jack.
For this, we will use the script below
$Date = Get-Date -Format "yyyy-MM-dd"
$ReportFolder = "C:\Reports"
if (!(Test-Path $ReportFolder)) {
New-Item -ItemType Directory -Path $ReportFolder
}
$ReportFile = "$ReportFolder\DailyReport_$Date.csv"
# Export AD Users
$Users = Get-ADUser -Filter * -Property SamAccountName, DisplayName, Enabled, LastLogonDate
$Users | Export-Csv $ReportFile -NoTypeInformation -Encoding UTF8
# Send Email with Report Attachment
$sendMailMessageSplat = @{
From = 'Administrator <administrator@techijack.net>'
To = 'Jack <jack@techijack.net>'
Subject = 'Daily User Report'
Body = 'Please find the attached user daily report.'
SmtpServer = 'mail.techijack.net'
Attachments = $ReportFile
}
Send-MailMessage @sendMailMessageSplat
You can set the report path, email address, and SMTP Server according to your settings.
You can test this code, and if it works, save it as a “DailyReport.ps1” script file.
As in our case, we save this script file inside the C drive.
We will run the script below
c:\dailyReport.ps1
Once the script runs successfully, an email will be sent along with a report attachment.
And our CSV report file will appear as below
So, in this way, you can send the daily report as an attachment in your email.
Consideration of Automation
If this is a daily task to get the information of the last logon for all the users in the organization.
You can try to automate the process with the help of the task scheduler and run the task on a daily basis.
Therefore, it will run the same script in a given interval of time, and automatically, you will get the daily report every day at a fixed time.
Conclusion
To make your administration work easier for the automatic reporting.
An Admin should use the multiple scripts to generate the report in a CSV or HTML.
Use PowerShell to run the script and send the report as an attachment to the responsible party.
Therefore, for automation, you can schedule the task to run the particular script at a specific time to get an email alert.
In case of any questions on this article, feel free to contact Techi Jack
I hope you will also like some other tutorials on Exchange Server
Moreover, if you want to watch the video for sending an Email from a PowerShell script, watch the video below.
