PoshCode Logo PowerShell Code Repository

Set-OutlookSignature.ps1 (modification of post by view diff)
View followups from Fabio Junior and Fabio Junior | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1565"></script>download | new post

Create Outlook signature based on user information from Active Directory. Settings for versioning, template-files, enforcing for New and ReplyForward are stored in the registry.

  1. ###########################################################################"
  2. #
  3. # NAME: Set-OutlookSignature.ps1
  4. #
  5. # AUTHOR: Jan Egil Ring
  6. #
  7. # COMMENT: Script to create an Outlook signature based on user information from Active Directory.
  8. #          Adjust the variables in the "Custom variables"-section
  9. #          Create an Outlook-signature from an Outlook-client based on your company template (logo, fonts etc) and copy this signature to the path defined in the $SigSource variable
  10. #          See the following blog-post for more information: http://blog.crayon.no/blogs/janegil/archive/2010/01/09/outlook-signature-based-on-user-information-from-active-directory.aspx
  11. #
  12. #          Tested on Office 2003,2007 and 2010 beta
  13. #
  14. # You have a royalty-free right to use, modify, reproduce, and
  15. # distribute this script file in any way you find useful, provided that
  16. # you agree that the creator, owner above has no warranty, obligations,
  17. # or liability for such use.
  18. #
  19. # VERSION HISTORY:
  20. # 1.0 09.01.2010 - Initial release
  21. #
  22. ###########################################################################"
  23.  
  24. #Custom variables
  25. $CompanyName = 'Company Name'
  26. $DomainName = 'domain.local'
  27. $SigVersion = '1.0' #When the version number are updated the local signature are re-created
  28. $SigSource = "\\$DomainName\netlogon\sig_files\$CompanyName"
  29. $ForceSignatureNew = '1' #When the signature are forced the signature are enforced as default signature for new messages the next time the script runs. 0 = no force, 1 = force
  30. $ForceSignatureReplyForward = '0' #When the signature are forced the signature are enforced as default signature for reply/forward messages the next time the script runs. 0 = no force, 1 = force
  31.  
  32. #Environment variables
  33. $AppData=(Get-Item env:appdata).value
  34. $SigPath = '\Microsoft\Signatures'
  35. $LocalSignaturePath = $AppData+$SigPath
  36.  
  37. #Get Active Directory information for current user
  38. $UserName = $env:username
  39. $Filter = "(&(objectCategory=User)(samAccountName=$UserName))"
  40. $Searcher = New-Object System.DirectoryServices.DirectorySearcher
  41. $Searcher.Filter = $Filter
  42. $ADUserPath = $Searcher.FindOne()
  43. $ADUser = $ADUserPath.GetDirectoryEntry()
  44. $ADDisplayName = $ADUser.DisplayName
  45. $ADEmailAddress = $ADUser.mail
  46. $ADTitle = $ADUser.title
  47. $ADTelePhoneNumber = $ADUser.TelephoneNumber
  48.  
  49. #Setting registry information for the current user
  50. $CompanyRegPath = "HKCU:\Software\"+$CompanyName
  51.  
  52. if (Test-Path $CompanyRegPath)
  53. {}
  54. else
  55. {New-Item -path "HKCU:\Software" -name $CompanyName}
  56.  
  57. if (Test-Path $CompanyRegPath'\Outlook Signature Settings')
  58. {}
  59. else
  60. {New-Item -path $CompanyRegPath -name "Outlook Signature Settings"}
  61.  
  62. $ForcedSignatureNew = (Get-ItemProperty $CompanyRegPath'\Outlook Signature Settings').ForcedSignatureNew
  63. $ForcedSignatureReplyForward = (Get-ItemProperty $CompanyRegPath'\Outlook Signature Settings').ForcedSignatureReplyForward
  64. $SignatureVersion = (Get-ItemProperty $CompanyRegPath'\Outlook Signature Settings').SignatureVersion
  65. Set-ItemProperty $CompanyRegPath'\Outlook Signature Settings' -name SignatureSourceFiles -Value $SigSource
  66. $SignatureSourceFiles = (Get-ItemProperty $CompanyRegPath'\Outlook Signature Settings').SignatureSourceFiles
  67.  
  68.  
  69. #Forcing signature for new messages if enabled
  70. if ($ForcedSignatureNew -eq '1')
  71. {
  72. #Set company signature as default for New messages
  73. $MSWord = New-Object -com word.application
  74. $EmailOptions = $MSWord.EmailOptions
  75. $EmailSignature = $EmailOptions.EmailSignature
  76. $EmailSignatureEntries = $EmailSignature.EmailSignatureEntries
  77. $EmailSignature.NewMessageSignature=$CompanyName
  78. $MSWord.Quit()
  79. }
  80.  
  81. #Forcing signature for reply/forward messages if enabled
  82. if ($ForcedSignatureReplyForward -eq '1')
  83. {
  84. #Set company signature as default for Reply/Forward messages
  85. $MSWord = New-Object -com word.application
  86. $EmailOptions = $MSWord.EmailOptions
  87. $EmailSignature = $EmailOptions.EmailSignature
  88. $EmailSignatureEntries = $EmailSignature.EmailSignatureEntries
  89. $EmailSignature.ReplyMessageSignature=$CompanyName
  90. $MSWord.Quit()
  91. }
  92.  
  93. #Copying signature sourcefiles and creating signature if signature-version are different from local version
  94. if ($SignatureVersion -eq $SigVersion){}
  95. else
  96. {
  97. #Copy signature templates from domain to local Signature-folder
  98. Copy-Item "$SignatureSourceFiles\*" $LocalSignaturePath -Recurse -Force
  99.  
  100. #Insert variables from Active Directory to rtf signature-file
  101. $MSWord = New-Object -com word.application
  102. $MSWord.Documents.Open($LocalSignaturePath+'\'+$CompanyName+'.rtf')
  103. ($MSWord.ActiveDocument.Bookmarks.Item("DisplayName")).Select()
  104. $MSWord.Selection.Text=$ADDisplayName
  105. ($MSWord.ActiveDocument.Bookmarks.Item("Title")).Select()
  106. $MSWord.Selection.Text=$ADTitle
  107. ($MSWord.ActiveDocument.Bookmarks.Item("TelephoneNumber")).Select()
  108. $MSWord.Selection.Text=$ADTelePhoneNumber
  109. ($MSWord.ActiveDocument.Bookmarks.Item("EmailAddress")).Select()
  110. $MSWord.Selection.Text=$ADEmailAddress
  111. ($MSWord.ActiveDocument).Save()
  112. ($MSWord.ActiveDocument).Close()
  113. $MSWord.Quit()
  114.  
  115. #Insert variables from Active Directory to htm signature-file
  116. $MSWord = New-Object -com word.application
  117. $MSWord.Documents.Open($LocalSignaturePath+'\'+$CompanyName+'.htm')
  118. ($MSWord.ActiveDocument.Bookmarks.Item("DisplayName")).Select()
  119. $MSWord.Selection.Text=$ADDisplayName
  120. ($MSWord.ActiveDocument.Bookmarks.Item("Title")).Select()
  121. $MSWord.Selection.Text=$ADTitle
  122. ($MSWord.ActiveDocument.Bookmarks.Item("TelephoneNumber")).Select()
  123. $MSWord.Selection.Text=$ADTelePhoneNumber
  124. ($MSWord.ActiveDocument.Bookmarks.Item("EmailAddress")).Select()
  125. $MSWord.Selection.Text=$ADEmailAddress
  126. ($MSWord.ActiveDocument).Save()
  127. ($MSWord.ActiveDocument).Close()
  128. $MSWord.Quit()
  129.  
  130.  
  131. #Insert variables from Active Directory to txt signature-file
  132. (Get-Content $LocalSignaturePath'\'$CompanyName'.txt') | Foreach-Object {$_ -replace "DisplayName", $ADDisplayName -replace "Title", $ADTitle -replace "TelePhoneNumber", $ADTelePhoneNumber -replace "EmailAddress", $ADEmailAddress} | Set-Content $LocalSignaturePath'\'$CompanyName'.txt'
  133. }
  134.  
  135.  
  136. #Stamp registry-values for Outlook Signature Settings if they doesn`t match the initial script variables. Note that these will apply after the second script run when changes are made in the "Custom variables"-section.
  137. if ($ForcedSignatureNew -eq $ForceSignatureNew){}
  138. else
  139. {Set-ItemProperty $CompanyRegPath'\Outlook Signature Settings' -name ForcedSignatureNew -Value $ForceSignatureNew}
  140.  
  141. if ($ForcedSignatureReplyForward -eq $ForceSignatureReplyForward){}
  142. else
  143. {Set-ItemProperty $CompanyRegPath'\Outlook Signature Settings' -name ForcedSignatureReplyForward -Value $ForceSignatureReplyForward}
  144.  
  145. if ($SignatureVersion -eq $SigVersion){}
  146. else
  147. {Set-ItemProperty $CompanyRegPath'\Outlook Signature Settings' -name SignatureVersion -Value $SigVersion}

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