Change script to look at text file for destination email addresses

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
100questionsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
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

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
100questionsAuthor Commented:
@oBdA - Thanks so much.  How would I separate the emails in the destination .txt file?
0
oBdACommented:
The usual: one address per line.
0
100questionsAuthor Commented:
Thanks very much, this is exactly what I was looking for, spot on.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.