Send email with Powershell script, schedule script with Windows Task Scheduler

Tags: powershell, SMTP

Here is a set of samples I used to do a common task in my scripting life.  This go around I wanted to use Powershell. 


A) Create a script to send emails in powershell
1) Open Powershell type set-executionpolicy Unrestricted 
‘This allows scripts to be run’
‘You’ll want to also look at using RemoteSigned

2) Type Notepad myscript.ps1

3) Paste sample code and save in myscript.ps1

$SmtpClient = new-object
$SmtpServer = “localhost”
$ =

$From = “Friendly Reminder <[email protected]>”
$To = [email protected]
$Title = “Subject Matter”
$Body = “Body Text” 

B) Testing Script
1) Type ./myscript.ps1
‘Verify you receive the email.  

C) How to schedule a powershell script in Windows Task Scheduler
1) Create a new scheduled task.

‘The syntax is to execute the script is:
powershell -command “& ‘SomeDirmyScript.ps1′” 

2) Set the script to run as your normal task scheduler accounts.


3) Execute task, verify you receive the email

Hope this helps,



  • http:// said

    <P>I am trying to use the same command in process can you please help me out here</P>
    <P>Dim oProcess As Process = New Process</P>
    <P>oProcess.StartInfo.UseShellExecute = False</P>
    <P>oProcess.StartInfo.RedirectStandardOutput = True</P>
    <P>oProcess.StartInfo.CreateNoWindow = False</P>
    <P>oProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal</P>
    <P>oProcess.StartInfo.FileName = "powershell.exe"</P>
    <P>Dim sArguments as string = powershell -command "& 'SomeDirmyScript.ps1'"</P>
    <P>oProcess.WaitForExit(1000 * iTimeOutSec)</P>

  • Anil said

    I get the following error

    C:WINDOWSsystem32WindowsPowerShellv1.0>powershell.exe c:myscript.ps1
    The term '[email protected]' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spel
    ling of the name, or if a path was included, verify that the path is correct and try again.
    At C:myscript.ps1:7 char:23
    + $To = [email protected] <<<<
    + CategoryInfo : ObjectNotFound: ([email protected]:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

    Exception calling "Send" with "4" argument(s): "The parameter 'to' cannot be an empty string.
    Parameter name: to"
    At C:myscript.ps1:10 char:17
    + $SmtpClient.Send <<<< ($from,$to,$title,$Body)
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

  • mahmoud said

    trying to send an email using powershell. I have a working code but when I substitute "From" and "To" to variables like "From=$source
    I get an error

    working code is

    $messageParameters = @{
    smtpServer = “smtphost”
    From = [email protected]
    To = [email protected]
    Subject = “You Submitted a Job”
    Body = “Your Job Chain is submitted successfully”
    Thanks for help 🙂

Add a Comment