Powershell formatting using send-mailmessage

I have created a powershell script that uses send-mailmessage and uses get-content to read in a file in html, however,  I want the message body to contain text and use the get-content to get the file so, it should say something like:-

Please see attached file

and then use the get-content to retrieve a html file

Hope this makes sense


Who is Participating?
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.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
You want to have text and file content both in the body, and no attachment?
A simple way is to just put it together - not pretty, but works:
$body = 'Please see attached file<br><br>'+(-join (get-content file.html))

Open in new window

Of course you should make sure to provide the -BodyAsHtml switch with your Send-MailMessage.

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
David Johnson, CD, MVPRetiredCommented:
you can't mix text/html with powershell.  You need to not use get content but simply use the -attachment option in your send-mailmessage i.e.
-Attachments "data.csv"
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
David, you can mix plain text and HTML, it just doesn't look that good.
Price Your IT Services for Profit

Managed service contracts are great - when they're making you money. Yes, you’re getting paid monthly, but is it actually profitable? Learn to calculate your hourly overhead burden so you can master your IT services pricing strategy.

David Johnson, CD, MVPRetiredCommented:
You misunderstood.. you cannot run a powershell script to a mail message and execute it in the mail message.. the body of an email message is text i.e.
start-process cmd.exe in the message will not ever execute
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
The question could be read indeed that way - sending over PowerShell code to get the content of the file. But that does not make much sense, so I went with the more useful interpretation.
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
I've doubts about what has been really meant here, so summarizing the available opitions.

Send the text file as attachment is not much different than to send it over as text in the HTML body, but doing the later is better for immediate review. Both options require the complete text to be stored in the local PST or OST, and if Exchange 2013 is used, one single time on the server (with auto dedup), with older Exchanges storing a copy for each receiver.

Sending over a PS script as attachment is possible but requires an inacceptable weakening of Outlook security settings to allow execution. PS scripts in email are already used for attacks.

Having local VBA code in Outlook requires some sophisticated setup, and to allow macro execution (again a security risk, though it can be restricted much better to be "secure"). Then a mail can contain a certain subject, some keywords in the body or whatever, and the file name to display. A button to call the (local) macro, e.g. located in the Quick Access bar, can be used to run the code to display the file.
This would keep the file in a single location, not having it contained in any mail. But the effort is worth it only for very large files, IMHO.
David Johnson, CD, MVPRetiredCommented:
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

From novice to tech pro — start learning today.