20 Creative Ways to Use Sample PowerShell to Send Output Over Email

PowerShell is a powerful scripting language that enables system administrators to automate tasks efficiently. Email functionality in PowerShell allows users to send important notifications directly through their scripts. By utilizing the Send-MailMessage cmdlet, administrators can easily relay output from their commands to designated recipients. Automating email notifications improves communication and enhances workflow management. Security protocols can also be integrated, ensuring that sensitive information remains protected during transmission.

20 PowerShell Examples for Sending Output Over Email

PowerShell is a powerful scripting language that allows system administrators to automate a variety of tasks—including sending reports or notifications via email. Here are 20 unique examples showcasing how to leverage PowerShell to send output via email for various scenarios.

1. Send Disk Space Report

This script sends an email summarizing the current disk space usage on your servers.


    $FreeSpace = Get-PSDrive C
    $Body = "Free Space on C Drive: $($FreeSpace.Free / 1GB) GB"
    Send-MailMessage -To "[email protected]" -From "[email protected]" -Subject "Disk Space Report" -Body $Body -SmtpServer "smtp.example.com"
    

2. Send Active Directory User List

This example retrieves and sends a list of all active users from Active Directory.


    $Users = Get-ADUser -Filter * | Select-Object Name, EmailAddress
    $Body = $Users | Out-String
    Send-MailMessage -To "[email protected]" -From "[email protected]" -Subject "Active Directory Users" -Body $Body -SmtpServer "smtp.example.com"
    

3. Send System Uptime Report

Keep track of server uptime with this script that reports system status via email.


    $Uptime = (Get-CimInstance -ClassName Win32_OperatingSystem).LastBootUpTime
    $Body = "System Uptime: $Uptime"
    Send-MailMessage -To "[email protected]" -From "[email protected]" -Subject "System Uptime Report" -Body $Body -SmtpServer "smtp.example.com"
    

4. Send Process List

This script compiles a list of currently running processes and sends it via email.


    $Processes = Get-Process | Select-Object Name, Id
    $Body = $Processes | Out-String
    Send-MailMessage -To "[email protected]" -From "[email protected]" -Subject "Active Processes" -Body $Body -SmtpServer "smtp.example.com"
    

5. Send Windows Updates Status

Monitor Windows updates neatly by sending an email summarizing installed updates.


    $Updates = Get-WindowsUpdate
    $Body = $Updates | Out-String
    Send-MailMessage -To "[email protected]" -From "[email protected]" -Subject "Windows Updates Status" -Body $Body -SmtpServer "smtp.example.com"
    

6. Send Network Configuration

This example retrieves the current network configuration and sends it out via email.


    $NetworkConfig = Get-NetIPAddress | Select-Object IPAddress, InterfaceAlias
    $Body = $NetworkConfig | Out-String
    Send-MailMessage -To "[email protected]" -From "[email protected]" -Subject "Network Configuration" -Body $Body -SmtpServer "smtp.example.com"
    

7. Send Event Log Report

Access the Windows Event Log and email the latest entries for review.


    $Logs = Get-EventLog -LogName Application -Newest 10
    $Body = $Logs | Out-String
    Send-MailMessage -To "[email protected]" -From "[email protected]" -Subject "Event Log Report" -Body $Body -SmtpServer "smtp.example.com"
    

8. Send CPU Usage Report

Track CPU performance easily by sending out the current usage statistics.


    $Cpu = Get-CimInstance Win32_Processor | Select-Object LoadPercentage
    $Body = "CPU Load: $($Cpu.LoadPercentage)%"
    Send-MailMessage -To "[email protected]" -From "[email protected]" -Subject "CPU Usage Report" -Body $Body -SmtpServer "smtp.example.com"
    

9. Send IIS Website Status

Monitor your IIS websites by sending a report on their current status.


    $IISSites = Get-IISSite | Select-Object Name, State
    $Body = $IISSites | Out-String
    Send-MailMessage -To "[email protected]" -From "[email protected]" -Subject "IIS Websites Status" -Body $Body -SmtpServer "smtp.example.com"
    

10. Send Last Login Time for Users

This script sends an email detailing the last login times for all users in Active Directory.


    $Users = Get-ADUser -Filter * | Select-Object Name, LastLogonDate
    $Body = $Users | Out-String
    Send-MailMessage -To "[email protected]" -From "[email protected]" -Subject "User Last Login Times" -Body $Body -SmtpServer "smtp.example.com"
    

11. Send Backup Status

Automate notifications on backup outcomes by sending status updates on your backups.


    $BackupStatus = Get-Content "C:\Backup\status.txt"
    Send-MailMessage -To "[email protected]" -From "[email protected]" -Subject "Backup Status" -Body $BackupStatus -SmtpServer "smtp.example.com"
    

12. Send Firewall Status

Keep your security measures in check with updates on the firewall status.


    $FirewallStatus = Get-NetFirewallProfile | Select-Object Name, Enabled
    $Body = $FirewallStatus | Out-String
    Send-MailMessage -To "[email protected]" -From "[email protected]" -Subject "Firewall Status" -Body $Body -SmtpServer "smtp.example.com"
    

13. Send Service Status Report

This script provides an overview of running Windows services and sends the information via email.


    $ServiceStatus = Get-Service | Where-Object {$_.Status -eq "Running"}
    $Body = $ServiceStatus | Out-String
    Send-MailMessage -To "[email protected]" -From "[email protected]" -Subject "Running Services" -Body $Body -SmtpServer "smtp.example.com"
    

14. Send Last Restart Time

Monitor server stability by reporting the last restart time through email notifications.


    $LastRestart = Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object LastBootUpTime
    $Body = "Last Restart: $LastRestart.LastBootUpTime"
    Send-MailMessage -To "[email protected]" -From "[email protected]" -Subject "Last Server Restart" -Body $Body -SmtpServer "smtp.example.com"
    

15. Send Custom Application Performance Metrics

Track the performance of your applications and send key metrics via email.


    $PerfStats = Get-Counter '\Process(*)\% Processor Time'
    $Body = $PerfStats.CounterSamples | Out-String
    Send-MailMessage -To "[email protected]" -From "[email protected]" -Subject "Application Performance Metrics" -Body $Body -SmtpServer "smtp.example.com"
    

16. Send Storage Utilization Summary

A summary of storage space utilization across volumes can be beneficial for capacity planning.


    $Storage = Get-PSDrive | Where-Object {$_.Provider -eq 'FileSystem'}
    $Body = $Storage | Out-String
    Send-MailMessage -To "[email protected]" -From "[email protected]" -Subject "Storage Utilization Summary" -Body $Body -SmtpServer "smtp.example.com"
    

17. Send Scheduled Task Status Report

This script sends the status of scheduled tasks for all users.


    $Tasks = Get-ScheduledTask | Select-Object TaskName, State
    $Body = $Tasks | Out-String
    Send-MailMessage -To "[email protected]" -From "[email protected]" -Subject "Scheduled Task Status Report" -Body $Body -SmtpServer "smtp.example.com"
    

18. Send VPN Connectivity Status

Sent status reports regarding VPN connectivity to keep teams informed.


    $VPNStatus = Get-VpnConnection
    $Body = $VPNStatus | Out-String
    Send-MailMessage -To "[email protected]" -From "[email protected]" -Subject "VPN Connectivity Status" -Body $Body -SmtpServer "smtp.example.com"
    

19. Send PowerShell Version Report

Remind users about the current version of PowerShell installed on their systems.


    $PSVersion = $PSVersionTable.PSVersion
    $Body = "PowerShell Version: $PSVersion"
    Send-MailMessage -To "[email protected]" -From "[email protected]" -Subject "PowerShell Version Report" -Body $Body -SmtpServer "smtp.example.com"
    

20. Send Security Updates Notification

This script provides updates regarding any available security patches for installed software.


    $SecurityUpdates = Get-WindowsUpdate | Where-Object {$_.Title -like "*Security*"}
    $Body = $SecurityUpdates | Out-String
    Send-MailMessage -To "[email protected]" -From "[email protected]" -Subject "Security Updates Notification" -Body $Body -SmtpServer "smtp.example.com"
    

How can PowerShell be utilized to automate email output delivery?

PowerShell is a powerful scripting language that can automate various tasks, including sending emails with output. To utilize PowerShell for email automation, you need to leverage the `Send MailMessage` cmdlet. This cmdlet requires specific parameters such as the sender’s email address, the recipient’s email address, the subject of the email, and the body content. When the command executes, it composes a message and delivers it to the specified recipient’s email address. In scenarios where output from a script or command needs to be emailed, the output can be captured and formatted into the email body, allowing for effective communication of results or logs. Thus, PowerShell serves as a robust solution for automating the process of sending output via email.

Also read:  20 Essential Tips for Sending Signed Offer Letter Email Sample

What are the essential components required to send email using PowerShell?

To send an email using PowerShell, several essential components must be configured correctly. First, the SMTP server address is required to route the email to its destination. Second, the sender’s email address provides the identity from which the email is sent. Third, the recipient’s email address specifies where the email should go. Additionally, the email subject and body content must be defined to convey the message clearly. Optionally, attachments can be included to share files relevant to the email content. By combining these components in a PowerShell script, users can generate and send emails effectively.

How can the output of a PowerShell command be formatted for email delivery?

Formatting the output of a PowerShell command for email delivery involves several steps to ensure clarity and readability. Primarily, the output needs to be captured as a string or an HTML format. The use of the `Out-String` cmdlet can convert standard output into string format, making it suitable for inclusion in the email body. Furthermore, for enhanced readability, the output can be formatted using HTML tags, including headers and line breaks. By assigning the formatted output to a variable, it can then be incorporated into the body parameter of the email. Ultimately, properly formatted output enhances communication, making it easier for recipients to understand the data conveyed in the email.

Well, there you have it! Sending outputs over email using PowerShell can be super handy for keeping everyone in the loop without any hassle. I hope you found the samples useful and feel inspired to try it out on your own projects. Thanks for hanging out and reading through this! Don’t be a stranger—swing by again soon for more tips and tricks. Happy scripting!