This is a script to replace placeholders like {name}
in a Word document with their values as defined in an Excel spreadsheet.
-
Save
replace.ps1
below somewhere on your machine. -
Create a Word document with your message, for example:
Hello {Name}, Your balance is {balance}
-
Create an Excel document with your replacements, and a header row, for example:
Name Balance John Doe $123.45 Suzan Pevensie $5456.70 -
Open PowerShell, you can either search for it or type
powershell
in the address bar in Windows Explorer. -
PowerShell will display the path before the arrow designating your input.
~
means your home folder C:\Users\You and..
means one directory up..
means the current directory. Usecd
to get to the directory you saved the script in.~> cd Documents ~\Documents> cd .. ~> cd Desktop ~\Desktop>
-
Run the script with
.\replace.ps1
You can either provide the arguments before hitting enter, or it will prompt you for them. You can use relative paths to the files instead of typing out the full path. If your files are all in the same directory, your call and the corresponding output could look like this. Note that in the output file name{Name}.docx
, you can use the column names from your Excel spreadsheet.~\Desktop\replace_test> .\replace.ps1 .\Template.docx .\Data.xlsx '{Name}.docx' Processing row 1/3 Processing row 2/3 Processing row 3/3 ~\Desktop\replace_test>
-
Open the generated output files and confirm the script worked.