Tuesday, 19 February 2008

Send Welcome Email

Wouldn't it be nice to send your new users an email welcoming them to the company and providing them with some info about how things work. Anyway, I had a request to do this and since I don't think you can do it natively within Exchange 2003 I turned straight away to Powershell.

So, all you need to do is run a daily sceduled task which calls the below Powershell script. It finds the current and next days's dates, searches AD for users created between those dates (i.e. on the current day) and sends an email to the users it finds with the relevant info attached.

(Note: You need the Quest AD cmdlets installed unless you want to use ADSI for the AD search part.)

Doing the search by specifiying the exact date didn't seem to work very successfully. Digging around the Internet revealed the correct format to get the dates into and also searching between the two adjacent dates.

#Find today's and tomorrow's dates and store them in the 'yyyyMMdd}000000.0Z' format
$dayStart = "{0:yyyyMMdd}000000.0Z" -f (Get-Date)
$dayEnd = "{0:yyyyMMdd}000000.0Z" -f ((Get-Date).AddDays(1))

#Find AD users created between the two dates
$newuser = Get-QADUser -searchroot 'springfield.local/userou' -LdapFilter "(&(whenCreated>=$dayStart)(whenCreated<=$dayEnd))" -size 0

#Find the email address for each of the users and send a mail to them with the WelcomeTo.doc attachment
foreach ($address in $newuser)
$recipient = $address.mail
$sender = "sender@domain.com"
$server = "smtpservername"
$file = "path to attachment"
$subject = "subject"
$body = "body text"
$msg = new-object System.Net.Mail.MailMessage $sender, $recipient, $subject, $body
$attachment = new-object System.Net.Mail.Attachment $file
$client = new-object System.Net.Mail.SmtpClient $server
$client.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials


Anonymous said...

Hello, How Do we use this in an Exchange 2003 Environment.
Basically am looking out for a Script to Send an automated Welcome email to New users.
Thanks in Advance.

Jonathan Medd said...

Then you have come to the right place because this script is for an Exchange 2003 environment.

Go try it!

Anonymous said...

Hi Jonathan

Thanks for the script. Just a question, is it possible to request a read receipt so I have proof that the user received the email.

Jonathan Medd said...

Looking at MSDN if you add the below it should add a read receipt.

$msg.Headers.Add("Disposition-Notification-To", "returnreceipt@return.com")


Anonymous said...

what would be the changes if we use ADSI for the AD search part.

Anonymous said...

Hi Jonathan,
what changes do i make to the script if im gonna use ADSI for the AD search part.

Jonathan Medd said...


You would need to do something like the following.

$searcher = New-Object DirectoryServices.DirectorySearcher

Anonymous said...

Hi Jonathan,

it works like a charm,
Thanks for sharing it with us.


Anonymous said...

Hello, I was curious as to whether the line:

Does this limit the search for new users to just one OU? Obviously I would change the springfield.local to my domain name. We have different GPO's pushed to different people and if I created a user in a OU that I created, would this script grab it? Sorry, I'm a script noob.