Populating Variables in External Text File

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
Posts: 6
Joined: Tue Nov 19, 2013 9:28 am

Populating Variables in External Text File

Post by pfgustafsson » Sun Apr 15, 2018 7:18 pm

Product, version and build: PowerShell Studio v5.5.150
32 or 64 bit version of product: 64 bit
Operating system: Win 10
32 or 64 bit OS: 64 Bit

*** Please add details and screenshots as needed below. ***


Here's a question I had after reading the blog post on storing powershell variables within an external text file.
https://www.sapien.com/blog/2018/03/22/ ... nal-files/

After the reading the post, I was also working on a project where I wanted to do the reverse of what the article was talking about and have variables within a text file grab variable values from within the script.

Here’s the scenerio:
I have a list of new mailbox accounts and want to inform all the users of their new email address and credentials (sent to their old email address). I want to utilize a ‘form style’ text document with html formatting for the -BodyAsHtml switch in the Send-MailMessage cmdlet and push variable values into the body of the message. I can successfully include the html formatted message in the body of the message however, none of the variables get populated. There must be away to read the message body first and inject the variable values before sending the message. Question is, how?

Any thoughts?

User avatar
Alexander Riedel
Posts: 7216
Joined: Tue May 29, 2007 4:43 pm

Re: Populating Variables in External Text File

Post by Alexander Riedel » Mon Apr 16, 2018 7:01 am

[Topic moved by moderator]
Alexander Riedel
SAPIEN Technologies, Inc.

Posts: 14157
Joined: Tue May 22, 2007 9:57 am

Re: Populating Variables in External Text File

Post by jvierra » Mon Apr 16, 2018 7:24 am


Code: Select all

$body = @'
    Dear  {0},
     Your free gift of {1} for being a dear customer
     has been shipped on {2}
$body -f 'Jane Dough', 'vanilla cake', '12/31/2031'
https://blogs.technet.microsoft.com/hey ... owershell/

The "-f" operator is the string formatter operator and works as a "sprintf" function.