PoshCode Logo PowerShell Code Repository

Create RTF File by Tango 3 weeks ago (modification of post by Sean Kearney view diff)
diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/6761"></script>download | new post

Very simple script to create an RTF (Rich Text Format) file with Windows Powershell with variable substitution. Yes this could be a very basic Mail Merge type document WITHOUT the use of Microsoft Word

  1. param (
  2. [string]$Filename
  3. )
  4. # Save script as NEWRTF.PS1
  5. #
  6. # Execute with ./NEWRTF.PS1 -filename somefilename.rtf
  7. #              
  8.  
  9. # Note for this basic example, there is no error checking
  10. # The full path name INCLUDING RTF extension must be supplied
  11. # for the filename
  12. #
  13. # For Example C:\Folder\Filename.RTF
  14. #
  15.  
  16. # Four static variables for an example
  17.  
  18. $Firstname="John"
  19. $Lastname="Smith"
  20.  
  21. # For an RTF file, you must "escape" the Backslash with an extra Backslash
  22.  
  23. $Accountname="CONTOSO\\jsmith"
  24. $Password="LousyPass123"
  25.  
  26. # Header of the RTF file
  27.  
  28. $Header+="{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1033{\fonttbl{\f0\fnil\fcharset0 Consolas;}}`r`n"
  29. $Header+="{\*\generator Riched20 6.2.8102}\viewkind4\uc1 `r`n"
  30. $Header+="\pard\sl276\slmult1\f0\fs22\lang9 \par`r`n"
  31.  
  32. # Content of the message
  33.  
  34. $Message+="Hello $Firstname $Lastname and Welcome to ABC\par`r`n"
  35. $Message+="Corporation.\par`r`n"
  36. $Message+="\par`r`n"
  37. $Message+="Your User ID is $Accountname\par`r`n"
  38. $Message+="Your Temporary Password is $Password\par`r`n"
  39. $Message+="\par`r`n"
  40. $Message+="Do not share this information and remember,\par`r`n"
  41. $Message+="We are watching....\par`r`n"
  42. $Message+="`r`n"
  43.  
  44. # Footer in the RTF File
  45.  
  46. $Footer="}`r`n"
  47.  
  48. # Build the content together
  49.  
  50. $Content=$Header+$Message+$Footer
  51.  
  52. # Create the file
  53.  
  54. ADD-CONTENT -path $Filename -value $Content -force

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me