Posted on May 11, 2017 · Posted in Active Directory, Outlook

Generating Outlook 2010/2013 Signature Using AD Information

In this article we’ll show how to automatically create a user signature in Outlook 2010/2013 based on data from  Active Directory. The following scenario will be considered: the first time a new domain user is logged on to the workstation, the PowerShell script automatically generates an Outlook user signature file with its contact information retrieved from Active Directory.

In order for this script to work correctly, it is necessary that all users in AD are filled with all the required attributes. In this example, we will use the following Active Directory attributes in the user’s signature:

  • Full username (in my case, these data are stored in the Description attribute)
  • Position (Title)
  • Company name (Company)
  • Postal code, city and address (PostalCode, City, StreetAddress)
  • Phone number (OfficePhone)
  • E-mail (Mail)
  • Website (Homepage)

Outlook 2010 - generated signature

You need to create 3 files with signature templates for Outlook in htm (HTML), rtf (Rich Text) and txt (Plain Text) formats. The design, contents and appearance of signature templates in these files have to comply with the requirements to corporate e-mail signature policy.

Create the file signature.htm containing the following HTML code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head></head>
<body>
<div id style="font-family:Arial&;color:#5B9BD5;">
<span style="font-size:10pt;color:#000000;">
<b><p>Best Regards</p>
<p>@NAME</p>      </span>
<span style="font-size:9.0pt;">
<p>&nbsp;</p>
<p>@DESCRIPTION</p>
<p>@COMPANY</p></b>
<p> &nbsp;</p></span>
<span style="font-size:8.0pt;">
<p> @POSTALCODE, @CITY, @STREETADDRESS</p>
<p> Phone.@OFFICEPHONE</p>
<p> <a href="http://@WEBSITE">@WEBSITE</a></p>
<p>e-mail:<a href="mailto:@EMAIL">@EMAIL</a></span>
</div>
</body>
</html>

signature html template

The contents of signature.rtf and signature.txt files should be as follows:

Best Regards,
@NAME
@DESCRIPTION
@COMPANY
@POSTALCODE, @CITY, @STREETADDRESS
Phone. @OFFICEPHONE
e-mail:@EMAIL
site:@WEBSITE

Create the folder OutlookSignature in C:\Users\Public\Downloads to store signature templates for Outlook and signatures of the computer users. In C:\Users\Public\Downloads\OutlookSignature create a subfolder Templates and copy three files containing signature templates to it. You can do it either manually or using Group Policy Preferences (GPP).

Outlook signature templates

Create a new file outlooksignature.ps1 with the following PowerShell code (I’ll give a short description of each block of code)

Determine the set of variables. The $User variable contains the name of the user, from whose account the script is run. Specify the names and extensions of the files as well as the paths to them in other variables.

$User = $env:UserName
$FileName = "signature"
$FileExtension = "htm","rtf","txt"
$Path = "C:\Users\Public\Downloads"
$PathSignature = "$Path\OutlookSignature"
$PathSignatureTemplates = "$Path\OutlookSignature\Templates"
$PathSignatureUser = "$PathSignature\$User"
$AppSignatures =$env:APPDATA + "\Microsoft\Signatures"

Import the PowerShell module to access AD. Using Get-ADUser cmdlet, get the values of the user attributes in Active Directory you need and save them in $AD_user object.

Note. In order to make Get-ADUser cmdlet work in Windows 7, RSAT has to be installed on your computer. The component Active Directory Module For Windows PowerShell  must also be enabled (Control Panel -> Programs and Features -> Turn On/Off Windows Features -> Remote Server Administration Tools  -> Role Administration Tools  -> AD DS And AD LDS Tools.

Import-module activedirectory
$AD_user = Get-ADUser $User -Properties Title,Company,Description,Fax,HomePage,Mail,OfficePhone,PostalCode,City,StreetAddress

Create a folder to store user signature files and copy the templates to it:

New-Item -Path "$PathSignature\$User" -ItemType Container –Force
foreach ($Ext in $FileExtension)
{
Copy-Item -Force "$PathSignatureTemplates\$FileName.$Ext" "$PathSignatureUser\$FileName.$Ext"
}

Then using replace feature, replace the data in the templates with user data from AD:

foreach ($Ext in $FileExtension)
{
(Get-Content "$PathSignatureUser\$FileName.$Ext") | Foreach-Object {
$_`
-replace "@NAME", $AD_user.Description `
-replace "@DESCRIPTION", $AD_user.title `
-replace "@COMPANY", $AD_user.Company `
-replace "@STREETADDRESS", $AD_user.StreetAddress `
-replace "@POSTALCODE", $AD_user.PostalCode `
-replace "@CITY", $AD_user.City `
-replace "@OFFICEPHONE", $AD_user.OfficePhone `
-replace "@EMAIL", $AD_user.Mail `
-replace "@WEBSITE", $AD_user.Homepage `
} | Set-Content "$PathSignatureUser\$FileName.$Ext"
}

Now you just have to copy files containing signature templates to the folder, in which Outlook 2010 / 2013 / 2016 stores signatures –  %APPDATA%\Microsoft\Signatures (C:\Users\username\AppData\Roaming\Microsoft\Signatures).

foreach ($Ext in $FileExtension)
{
Copy-Item -Force "$PathSignatureUser\$FileName.$Ext" "$AppSignatures\$User.$Ext"
write-host "$PathSignatureUser\$FileName.$Ext"
write-host "$AppSignatures\$User.$Ext"
}

To make Outlook use these files with signature templates when started, do the following:

  1. Delete First-Run  value from HKEY_CURRENT_USER\Software\Microsoft\Office\< Office_Version>\Outlook\Setup
  2. In HKEY_CURRENT_USER\Software\Microsoft\Office\< Office version>\Common\MailSettings create two string values with the names NewSignature and ReplySignature, which will contain the template name and the signature (in our case the template name is identical to AD account name)

To work in different MS office versions, you have to add this code:

#Office 2010
If (Test-Path HKCU:'\Software\Microsoft\Office\14.0') {
Remove-ItemProperty -Path HKCU:\Software\Microsoft\Office\14.0\Outlook\Setup -Name First-Run -Force -ErrorAction SilentlyContinue -Verbose
New-ItemProperty HKCU:'\Software\Microsoft\Office\14.0\Common\MailSettings' -Name 'ReplySignature' -Value $User -PropertyType 'String' -Force
New-ItemProperty HKCU:'\Software\Microsoft\Office\14.0\Common\MailSettings' -Name 'NewSignature' -Value $User -PropertyType 'String' -Force
}
#Office 2013
If (Test-Path HKCU:'\Software\Microsoft\Office\15.0') {
Remove-ItemProperty -Path HKCU:\Software\Microsoft\Office\15.0\Outlook\Setup -Name First-Run -Force -ErrorAction SilentlyContinue -Verbose
New-ItemProperty HKCU:'\Software\Microsoft\Office\15.0\Common\MailSettings' -Name 'ReplySignature' -Value $User -PropertyType 'String' -Force
New-ItemProperty HKCU:'\Software\Microsoft\Office\15.0\Common\MailSettings' -Name 'NewSignature' -Value $User -PropertyType 'String' -Force
}

powershell script to genarate outlook signature based on active directory user information

Just make this PowerShell script to run once using Group Policy Preferences when a user login to the system. As a result, Outlook will use the signature we created for the outgoing e-mails. The first image of the article shows a sample of this signature.

Some tips

  • If Outlook displays HTM signature with large (double) spaces between lines, it is the Outlook bug. It’s better to create an HTM signature right in Outlook, and use it as the template (stored in %APPDATA%\Microsoft\Signatures)
  • You can also add a user photo from thumbnailPhoto attribute of Active Directory to the signature. Since there isn’t any simple way to add an image to Outlook signature, like in the item above, it is better to create a signature template containing any image in Outlook and by copying replace the image file in the template folder in the PowerShell script (the image is stored in %AppData%\Microsoft\Signatures\<signature name>.files).
  • In Exchange 2007 and higher, you can make the simplest signature inserted in all emails using Transport Rules

Previous:
Next:
Related Articles