Petri Newsletter Sign-up
Office 365 Insider

Here at, we get IT — and so can you. Subscribe today to stay informed and knowledgeable regarding the latest on IT.

    See All Petri Newsletters

    Generate Test Data for Exchange 2010 Using PowerShell: Sending Email

    Posted on by Jeff Hicks in PowerShell

    In a previous article I guided you through a technique to generate user logons in a test Exchange 2010 server. Certainly the heart of any testing will center around mailboxes and storage groups. The challenge is to populate mailboxes with data. This is easier than you might imagine using PowerShell.

    I’m not going to mess with Outlook or even Outlook Web Access, so my techniques won’t create perfect simulations of a user’s mailbox, but it will fill it with as much data as I want, which for my purposes is all I care about. When I run a command like Get-MailboxStatistics I want to see something. I hope it goes without saying, but these techniques are for use in a test and non-production environment. And they are certainly not the only way. But all I need is PowerShell on a client machine.

    Creating Test Data: Send Mail Message

    In PowerShell we can use the Send-MailMessage cmdlet and essentially spam the test Exchange server with mail messages.

    There are steps you can do to define the SMTP server name in your PowerShell session, but I wanted to show the complete command. I sent a message to myself that will appear to come from Jack Frost.

    Exchange 2010 TestData

    The message won’t show up as a sent item in Jack’s mailbox, but I don’t care about that. The point is that I was able to generate a mail message and Exchange delivered it.

    Scaling Up

    The great thing about PowerShell is that if I can do something once I can do it 10, 100, or 1,000 times. To load up my server with test messages all I need to do is get user accounts from Active Directory and send mail messages. Let’s look at this step by step.

    First I’m going to get all enabled users with mailboxes in the Employees OU from my domain.

    I’m running this command from a Windows 8 desktop with RSAT installed. It should work from PowerShell 2 on Windows 7 as long as you have the ActiveDirectory module installed.

    I’ll get a random user as the sender.

    Because people often email to multiple recipients, and to help build data, I’ll send to random number of recipients, but no more than five.

    Now, I need a subject and a body, but I want it to be a somewhat random (yet reasonable) size. So I decided to use the About topics that have a synopsis defined. There are some that don’t.

    With this, I can get a random topic.

    All that remains is to send the mail message.

    This will work in PowerShell 3.0 because even though $sendto is a collection of user objects, PowerShell 3.0 will automatically turn the Mail property into an array of mail addresses. In 2.0 I’d have to try something like this.

    The subject will be the topic name and the mail body will be the synopsis. I can repeat this process for as many users as I want and in no time I have populated mailboxes.

    You can see my code below, but you can also click on the link to download my Add-MailData script.

    #requires -version 3.0



    This script is designed to populate a test Exchange server

    with mail messages. The script can be run from a client

    desktop that has the Active Directory module installed.



    Param (


    [string]$PSEmailServer = “chi-ex01.globomantics.local”,

    #number of messages to create

    [int]$Count = 25



    #import the module

    Import-Module ActiveDirectory



    #use about help topics as message bodies

    $topics = Get-help about* | where {$_.synopsis}


    Try {

    #get all enabled users with mailboxes in the Employees OU

    $OUPath = “OU=Employees,DC=globomantics,DC=local”

    $users = get-aduser -filter {mail -like “*” -AND Enabled -eq $True} -properties mail -SearchBase $OUPath -ErrorAction Stop


    Catch {

    Write-Warning “Failed to find any mail-enabled user accounts in $OUPath. $($_.Exception.Message)”




    #only proceed if users were found

    if ($users.count -ge 1) {


    0..$count | foreach {


    $per = ($_/$count)*100

    #get a random user

    $sender = $users | Get-Random

    #get a random number of users, between 1 and 5

    #to send the message to

    $numUsers = Get-Random -Minimum 1 -Maximum 5

    $sendto = $users | Get-Random -Count $numUsers


    #get a random help topic

    $topic = $topics[(get-random -Minimum 0 -Maximum $topics.count)]


    Write-Progress -Activity “Generating Mail Data” -Status “Item $_”

    -CurrentOperation "From $($sender.mail) Subject re: $($" -percentComplete $per


    #send the mail message

    Send-MailMessage -To $sendto.mail -From $sender.mail

    -Subject “re: $($” -body ($topic.Synopsis | out-string)

    #insert a random time offset so not all the messages show up

    #at once.

    $sleep = Get-Random -Minimum 1 -Maximum 30

    Start-Sleep -Seconds $sleep


    } #foreach

    } #if users found

    I can run this as often as I want for as many times as I want. It doesn’t take too long and of course you could always run it as a background job. For me, the end result is data.

    Exchange 2010 Test Data

    The script puts some work on the Exchange server, which is helpful if I’m testing something with performance data.



    Don't have a login but want to join the conversation? Sign up for a Petri Account