Powershell - Send-MailMessage - not sending to a variable -To recepient ($MailRecepient = $SamAccountName + "@abc.ie")

Nelson Figueroa 31 Reputation points

All part of the script is working except the addressee in the "-To" portion is not getting the email. Below is the script:

Script for new-aduser here…then email user

$SamAccountName = "jlazy"
$Name = "John Lazy"
$MailRecepient = $SamAccountName + "@jaswant .ie" <<<<<<<<<<<<THIS
$mailsubject = "Domain Account Created - " + $Name
$mailBody = "Dear " + $Name + ",rnrn"
$mailBody += "Your domain account has been created.rnrn"
$mailBody += " username: " + $SamAccountName + "rn"
$mailBody += " password: password1234 rnrn"
$mailBody += "Please change your password right away at any computer in the hospital by logging in or by pressing Ctrl + Alt + Delete on the keyboard.rnrn"
$mailBody += "Your IT Department"

Send-MailMessage -From 'Account Creator<no-reply@abc.ie>'
-To $MailRecepient <<<<<<<<<<<<<<<THIS -Cc 'helpdesk@abc.ie'
-Subject $mailsubject -Body $mailBody
-SmtpServer "" `
-Port "587"

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,459 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 45,831 Reputation points

    You've misspelled "$MailRecipient" as "$MailRecepient" in the value supplied for the "-To" parameter.

    When you run into problems you don't understand, try adding Set-PSDebug -Strict at the top of the script and then run the code. Or, better, use an IDE like VS Code (with the PowerShell Pro Tools extension), or the free version of Visual Studio for your development work. They include tools that will point out errors like that before you run the code.

    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Rich Matheisen 45,831 Reputation points

    Is it safe to assume that the SMTP address "no-reply@jaswant .ie" exists? If it does, is there a Non-Delivery Report (NDR) in the mailbox?

    If that address doesn't accept e-mail, check the SMTP protocol log on the SMTP server at the IP address

    Are you getting an error message when the Send-MailMessage runs? If not, the message was accepted for delivery by the SMTP server. It's now the responsibility of the SMTP server to either deliver the message to the recipient, pass the message to another server (i.e., relay the message), or to send a NDR to the sender of the message.

    0 comments No comments

  2. Nelson Figueroa 31 Reputation points

    The Send-MailMessage works, that is it is sending emails to the -Cc addressee 'helpdesk@jaswant .ie'. So all the technicalities you mentioned are not the problem.

    The problem is the "-To" addressee is not receiving email.

    I generate the email address by $MailRecepient = $SamAccountName + "@jaswant .ie". Then plug the $MailRecepient to the -To argument like so "-To $MailRecepient". I need to supply a none static email address.

    If I replaced the "-To $MailRecepient" with "-To 'someone@jaswant .ie'", that 'someone@jaswant .ie' will receive the email.

    Is variable like $MailRecepient allowed in the "-To" argument?

    0 comments No comments

  3. Nelson Figueroa 31 Reputation points

    Thanks for your help, I certainly learned from your comments.

    0 comments No comments