Scripts, Tech Random's, Tips and Tricks

Send an Email Using Command Prompt or Batch File Script with PowerShell

This will allow you to to call a powershell script from a batch file that creates and sends an email.

You will need:

  1. SMTP Outgoing Server
  2. SMTP Outgoing Credentials

Apply this line to any Batch file line.

Our example:

robocopy C:\Data D:\Data /L /MIR /Z /Copy:DAT /DCOPY:T /J /mt /tee /r:1 /W:1 /ETA /xd “$RECYCLE.BIN” /log+:”C:\RCLogs.txt”

PowerShell.exe -NoProfile -ExecutionPolicy Bypass C:\UsersbackupsDesktopemail.ps1

Step 1:

Create a new text file and save it as a .PS1, enter the following, changing as needed.
—–
#this works, just sends a simple email
$smtpServer = “mail.smtp2go.com”
$smtpFrom = “FROMEMAILENTERHERE”
$smtpTo = “TOEMAILENTERHERE”
$smtpSubject = “This is my email subject”
$username = “email_username”
$password = “email_password”
$body = “yo yo yo your boat”

$smtp = New-Object -TypeName “Net.Mail.SmtpClient” -ArgumentList $smtpServer
$smtp.Credentials = New-Object system.net.networkcredential($username, $Password);
$smtpBody = “[$(Get-Date -Format HH:mm:ss)] $body”
$smtp.Send($smtpFrom, $smtpTo, $smtpSubject, $smtpBody)

—–

You can send this either as a regular powershell script or call it from a command line as shown above.

Leave a Reply

Your email address will not be published.