Change script to look at text file for destination email addresses

100questions
100questions used Ask the Experts™
on
I would like to change this script, so that instead of hard coding in the destination email addresses, it will look into a flat txt file and use the email addresses listed in that specific file instead.


###SETUP START###
#-------DO NOT MODIFY-------#
    #Yesterdays Date
        $date = ((Get-Date).AddDays(-2)).ToString("MMM/dd/yyyy")
###SETUP END###

###USER VARIABLES START###
#-------MODIFY AS NEEDED-------#
    #Mail Subject
        $Subject = "Report: "+$date+" REVISED "
    #Body of Email
        $MessageBody = "REVISED - Report for $date. Thank you."
###USER VARIABLES END###

###PROGRAM START###
      # Invokes the Send-MailMessage function to send notification email
Send-MailMessage -From 'email1@mail.com' -To 'email2@mail.com', 'email3@mail.com', 'email4@mail.com' `
  -SmtpServer 'smtp.mail.com' `
  -Subject  $subject `
  -body $MessageBody `
  -BodyAsHtml `
  -Attachment 'C:\Attachment\Attachment.pdf'
 ###PROGRAM END###

Thanks in advance for your assistance
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
Since -To accepts an array, and that is exactly what Get-Content returns, all you need is to set -To to the the return value of a "Get-Content" command.
And to avoid line continuation characters on long command lines, you can use something called "splatting", where you collect the arguments in a hash table, and then pass the hash table as argument, but with a @ instead of the $ (you can combine tat with immediate arguments as well):
###SETUP START###
#-------DO NOT MODIFY-------#
	#Yesterdays Date
	$date = ((Get-Date).AddDays(-2)).ToString("MMM/dd/yyyy")
###SETUP END###

###USER VARIABLES START###
#-------MODIFY AS NEEDED-------#
#Mail Subject
	$Subject = "Report: "+$date+" REVISED "
	#Body of Email
	$MessageBody = "REVISED - Report for $date. Thank you."
###USER VARIABLES END###

###PROGRAM START###
	# Invokes the Send-MailMessage function to send notification email
	$splat = @{
		From =			'email1@mail.com'
		To =			Get-Content -Path 'C:\Temp\destination.txt'
		SmtpServer =	'smtp.mail.com'
		Subject =		$subject
		body =			$MessageBody
		BodyAsHtml =	$true
		Attachment =	'C:\Attachment\Attachment.pdf'
	}
	Send-MailMessage @splat
###PROGRAM END###

Open in new window

Author

Commented:
@oBdA - Thanks so much.  How would I separate the emails in the destination .txt file?
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
The usual: one address per line.

Author

Commented:
Thanks very much, this is exactly what I was looking for, spot on.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial