I used the following PowerShell script to make new AD users, and after the user is created, a user id and password are displayed in Powershell. Now, I will like to have this userID and password to be sent automaticall to the new user with a pdf attachment on how to reset the password using an SMTP server.

# Import active directory module for running AD cmdlets
Import-Module activedirectory

#Store the data from user.csv in the $user variable
$ADUsers = Import-csv d:useraccount\user.csv

#Loop through each row containing user details in the CSV file 
foreach ($User in $ADUsers)
    #Read user data from each field in each row and assign the data to a variable as below

    $FirstName      = $User.FirstName
    $Initials       = $User.Initials
    $Surname        = $User.LastName
    $DisplayName    = $User.DisplayName
    $Path           = $User.Path 
    $SamAccountName = $User.SamAccountName
    $Password       = $User.Password
    $Description    = $User.Description

    if (Get-ADUser -F {SamAccountName -eq $SamAccountName})
         #If user does exist, give a warning
         Write-Warning "A user account with Logon Name $SamAccountName already exist in Active Directory."
        #User does not exist then proceed to create the new user account

        #Account will be created in the OU provided by the $OU variable read from the CSV file
        New-ADUser `
            -SamAccountName $SamAccountName `
            -UserPrincipalName "$SamAccountName@DCNAME" `
            -Name "$Firstname $Lastname" `
            -GivenName $Firstname `
            -Surname $Lastname `
            -Initials $Initials `
            -DisplayName $DisplayName `
            -Description $Description `
            -AccountPassword (convertto-securestring $Password -AsPlainText -Force) -ChangePasswordAtLogon $True
             add-adgroupmember $MemberGroup $SamAccountName 


My Question is, has anyone does this of have a sample script that i can reference to complete this task. I have installed and configured SMTP, but not really sure how to add a script to trigger the email upon new user creation.

  • and your question is?
    – longneck
    Oct 30, 2019 at 20:02
  • Hi, you tried what ? as wrote it seem a request to do it for you
    – yagmoth555
    Oct 30, 2019 at 20:02
  • Sorry guys, didn't know some part of my question got deleted. I have updated the post; hope this helps.
    – Sunny J
    Oct 30, 2019 at 21:18
  • Your script only creates an user account; there's nothing actually trying to send an email there. You should have at least tried a Google search (such as "powershell smtp") before asking.
    – Massimo
    Oct 30, 2019 at 21:45
  • Also, your question title is misleading; what you are actually looking for is "how to send an email from a PowerShell script", the fact that it contains user account details is not relevant to the task of sending an email.
    – Massimo
    Oct 30, 2019 at 22:35

1 Answer 1


PowerShell has built-in support for sending SMTP messages: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/send-mailmessage.

Basic syntax:

Send-MailMessage -From 'User01 <user01@fabrikam.com>' -To 'User02 <user02@fabrikam.com>' -Subject 'Test mail'

More detailed example (taken from the same article), which also includes an attachment and the mail server to use:

Send-MailMessage -From 'User01 <user01@fabrikam.com>' -To 'User02 <user02@fabrikam.com>' -Subject 'Sending the Attachment' -Body "Forgot to send the attachment. Sending now." -Attachments .\data.csv -SmtpServer 'smtp.fabrikam.com'

BTW, you don't need to install a SMTP server in order to send emails, and it probably won't work unless you properly configure it and several other things (such as SPF); it's a lot easier to use an existing one.

  • Thanks Massimo for your comment, regarding googling this. I'm actually, checking the steps here: blog.netwrix.com/2017/03/30/… I will let you know if i can get. I know that this can be done with PS, but I was hoping to use smpt instead.
    – Sunny J
    Oct 30, 2019 at 22:00
  • 1
    What do you mean by "I was hoping to use SMTP"? Just use the Send-MailMessage cmdlet and point it to any SMTP server you want. And yes, you can point it to localhost if you really want to run your own SMTP server.
    – Massimo
    Oct 30, 2019 at 22:18
  • Thanks, everyone. Got this working.
    – Sunny J
    Nov 1, 2019 at 2:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.