This script could be used to send welcome mail for each newly created mailbox in Microsoft Exchange 2010. This script allows you to use an .html file as a template. You can use placeholders in this template file. This script allows you to replace these placeholders when the script is being executed.
In a real world, IT administrators are always required by human resource department to send welcome mail to each new employee. A welcome mail to a new employee who has accepted your job offer confirms the employee's decision to accept the position. It will save the new employee some feelings of insecurity and avoids misunderstandings.
This script contains one advanced function, Send-OSCEXWelcomeMail. It will create a log file (SentLog.csv) in the same folder where the script resides. If you want to run the script periodically, you should prepare a mail enabled user account which will be used to send welcome mails. And you should grant this account with proper permission, like Recipient Management, by using Role Base Access Control (RBAC) User Editor. Otherwise this account cannot run the cmdlets which are required by this script.
Also you should prepare an .html welcome mail template by using Word or any other web page editor.
You can use this script in following way:



Here are some code snippets for your references. To get the complete script sample, please click the download button at the beginning of this page.
if ($sentUsers.ContainsKey($newlyCreatedMailboxAlias)) {
$verboseMsg = $Messages.WelcomeMailHasBeenSentAlready
$verboseMsg = $verboseMsg -f $newlyCreatedMailboxAlias
$pscmdlet.WriteVerbose($verboseMsg)
} else {
Switch ($pscmdlet.ParameterSetName) {
"__AllParameterSets" {
#Send welcome mail to one employee
$verboseMsg = $Messages.SendingWelcomeMail
$verboseMsg = $verboseMsg -f $newlyCreatedMailboxAlias
$pscmdlet.WriteVerbose($verboseMsg)
if ($Credential -ne $null) {
Send-MailMessage -From $From -To $($newlyCreatedMailbox.PrimarySmtpAddress) `
-Subject $Subject -Body $mailBody -BodyAsHtml `
-SmtpServer $SMTPServer -Credential $Credential
} else {
Send-MailMessage -From $From -To $($newlyCreatedMailbox.PrimarySmtpAddress) `
-Subject $Subject -Body $mailBody -BodyAsHtml `
-SmtpServer $SMTPServer
}
#Write user alias and mail sent time to the log file
Out-File -FilePath $scriptLogFile -InputObject "`"$newlyCreatedMailboxAlias`",`"$(Get-Date)`"" -Append -Encoding Unicode
}
"Pilot" {
#Redirect welcome mail to administrator
$verboseMsg = $Messages.RedirctingWelcomeMail
$verboseMsg = $verboseMsg -f $newlyCreatedMailboxAlias,$AdminEmailAddress
$pscmdlet.WriteVerbose($verboseMsg)
if ($Credential -ne $null) {
Send-MailMessage -From $From -To $AdminEmailAddress `
-Subject $Subject -Body $mailBody -BodyAsHtml `
-SmtpServer $SMTPServer -Credential $Credential
} else {
Send-MailMessage -From $From -To $AdminEmailAddress `
-Subject $Subject -Body $mailBody -BodyAsHtml `
-SmtpServer $SMTPServer
}
}
}
}
if ($sentUsers.ContainsKey($newlyCreatedMailboxAlias)) { $verboseMsg = $Messages.WelcomeMailHasBeenSentAlready $verboseMsg = $verboseMsg -f $newlyCreatedMailboxAlias $pscmdlet.WriteVerbose($verboseMsg) } else { Switch ($pscmdlet.ParameterSetName) { "__AllParameterSets" { #Send welcome mail to one employee $verboseMsg = $Messages.SendingWelcomeMail $verboseMsg = $verboseMsg -f $newlyCreatedMailboxAlias $pscmdlet.WriteVerbose($verboseMsg) if ($Credential -ne $null) { Send-MailMessage -From $From -To $($newlyCreatedMailbox.PrimarySmtpAddress) ` -Subject $Subject -Body $mailBody -BodyAsHtml ` -SmtpServer $SMTPServer -Credential $Credential } else { Send-MailMessage -From $From -To $($newlyCreatedMailbox.PrimarySmtpAddress) ` -Subject $Subject -Body $mailBody -BodyAsHtml ` -SmtpServer $SMTPServer } #Write user alias and mail sent time to the log file Out-File -FilePath $scriptLogFile -InputObject "`"$newlyCreatedMailboxAlias`",`"$(Get-Date)`"" -Append -Encoding Unicode } "Pilot" { #Redirect welcome mail to administrator $verboseMsg = $Messages.RedirctingWelcomeMail $verboseMsg = $verboseMsg -f $newlyCreatedMailboxAlias,$AdminEmailAddress $pscmdlet.WriteVerbose($verboseMsg) if ($Credential -ne $null) { Send-MailMessage -From $From -To $AdminEmailAddress ` -Subject $Subject -Body $mailBody -BodyAsHtml ` -SmtpServer $SMTPServer -Credential $Credential } else { Send-MailMessage -From $From -To $AdminEmailAddress ` -Subject $Subject -Body $mailBody -BodyAsHtml ` -SmtpServer $SMTPServer } } } }
Example 01: Displays help about Send-OSCEXWelcomeMail
Command: Get-Help Send-OSCEXWelcomeMail -Full
Screenshot:

Example 02: Send welcome mails to these mailboxes which created four hours before the script is being executed. (In pilot mode)
If the script is executed in pilot mode, it means welcome mails will not be sent to the newly created mailboxes. These welcome mails will be redirected to the administrator's mailbox. It’s useful for verifying the functionality of the script.
Command:
Send-OSCEXWelcomeMail -ScheduledTaskRepeatInterval 240 -From "itadmin@corp.contoso.com" -Subject "Welcome to Contoso!" -SMTPServer "smtpserver.corp.contoso.com" -Template "C:\Scripts\029\MailTemplate.html" -AdminEmailAddress
"itadmin@corp.contoso.com" -Verbose
Screenshot:

Note: Duplicate welcome mails may send to administrator’s mailbox if you run this command multiple times in a short time period(less than the value of ScheduledTaskRepeatInterval). Because of administrators may want to adjust the format of welcome mail, this behavior is by design. The root cause of this behavior is if the script is executed in pilot mode, it will not write a log entry in the log file.
Example 03: Send welcome mails to these mailboxes which created four hours before the script is being executed.
Command:
Send-OSCEXWelcomeMail -ScheduledTaskRepeatInterval 240 -From "itadmin@corp.contoso.com" -Subject "Welcome to Contoso!" -SMTPServer "smtpserver.corp.contoso.com" -Template "c:\Scripts\029\MailTemplate.html" -Verbose
Screenshot:

Example 04: Send welcome mails to these mailboxes which CustomAttribute1 is ITO. The mailboxes are created four hours before the script is being executed. Replace Placeholder01 which contains in the template file with the real department
name.
Command:
Send-OSCEXWelcomeMail -MailboxFilter 'CustomAttribute1 -eq "ITO"' -ScheduledTaskRepeatInterval 240 -From "itadmin@corp.contoso.com" -Subject "Welcome to Contoso!" -SMTPServer "smtpserver.corp.contoso.com" -Template
"c:\Scripts\029\MailTemplate.html" -ReplacePlaceholders @{"Placeholder01"="ITO department"} -Verbose
Screenshot:

Technical Resources:
Windows PowerShell Advanced Function
Get-Mailbox
Filterable Properties for the -Filter Parameter in Exchange 2007 SP1 and SP2
Scripting with the Exchange Management Shell
Understanding Role Based Access Control