Company-Wide Email Signatures on Windows Server


Consistency in email signatures across an organization is crucial for maintaining a professional image. Manually creating and updating these signatures can be a laborious and error-prone task. This article provides all scripting for this process using PowerShell to create a CSV file on Windows Server. To see the process in action, check out my YouTube video.

PersonalizeSignature.ps1


function CreateSignature($inputFilename, $outputFilename) {
    # Define replacement strings as an array
    $replacements = @(
        @{ Name = "GivenName"; Value = "{{First Name}}" },
        @{ Name = "Surname"; Value = "{{Last Name}}" },
        @{ Name = "Department"; Value = "{{Department}}" },
        @{ Name = "Title"; Value = "{{Title}}" },
        @{ Name = "MobilePhone"; Value = "{{Mobile}}" },
        @{ Name = "homePhone"; Value = "{{Ext}}" }
    )

    # Import the CSV file from a network path
    $csvPath = ".\company.users.csv"
    $userInfo = Import-Csv -Path $csvPath

    # Get the current user's username from the environment variable
    $currentUsername = $env:USERNAME

    # Find the user's information from the CSV file
    $currentUser = $userInfo | Where-Object { $_.Username -eq $currentUsername }

    # Replace the placeholders in the HTML signature template located on the network path
    $htmlSignature = Get-Content -Path $inputFilename -Raw

    # Loop through the replacements array and replace the placeholders with the corresponding user data or an empty string if not found
    foreach ($replacement in $replacements) {
        $replacementValue = ""
        if ($replacement.Name -eq "GivenName" -and ($currentUser.Title -eq "" -or $currentUser.MobilePhone -eq "") -and $currentUser.GivenName -ne "") {
            $replacementValue = "<br>$($currentUser.GivenName)"
        }
        elseif ($replacement.Name -eq "Title" -and $currentUser.Title -eq "" -and $currentUser.MobilePhone -eq "") {
            $replacementValue = "<br>"
        }
        elseif ($currentUser -and $currentUser.$($replacement.Name)) {
            $replacementValue = $currentUser.$($replacement.Name)
        } elseif (-not $currentUser) {
            $replacementValue = ""
        } 

        if ($replacement.Name -eq "homePhone") {
            if ($currentUser.homePhone -eq "") {
                $replacementValue = ""
            } else {
                $replacementValue = "Ext.$($currentUser.homePhone)"
            }
        }

        $htmlSignature = $htmlSignature -replace $replacement.Value, $replacementValue
    }

    # Save the personalized HTML signature to the output file
    Set-Content -Path $outputFilename -Value $htmlSignature
}

#Create the signature
$inputFilename = "\\company.local\IT\Config\ComapnySignature.htm"
$outputFilename = "$env:APPDATA\Microsoft\Signatures\CompanySignature.htm"
CreateSignature $inputFilename $outputFilename

PersonalizeSignature.vbs

Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "powershell.exe -WindowStyle Hidden -ExecutionPolicy Bypass -File \\SERVER1\ITScripts\PersonalizeSignature.ps1", 0

UpdateUserInfo.vbs

#Define the Admin user (Not from AD)
$AdminUser = '"Administrator","Company","IT","Cloud Services","",""'

# Import the Active Directory module if not already loaded
if (!(Get-Module -Name ActiveDirectory)) {
    Import-Module ActiveDirectory
}

# Define the output CSV file path
$outputCsvPath = ".\company.users.csv"

# Get user information from Active Directory
$users = Get-ADUser -Filter * -Properties GivenName, Surname, Title, MobilePhone, homePhone, UserPrincipalName, Department

# Filter users with a UPN, remove the domain from the UPN, and export to a CSV file
$users |
Where-Object { $_.UserPrincipalName -ne $null } |
Select-Object @{Name = 'Username'; Expression = {$_.UserPrincipalName -replace '@rival.local'}},
              GivenName, Surname, Title, MobilePhone, homePhone, Department |
Export-Csv -Path $outputCsvPath -NoTypeInformation

#Add Admin User at the end
Add-Content -Path $outputCsvPath -Value $AdminUser

CompanySignature.htm

<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>Company Email Signature</title>
</head>
<body style="font-family: Arial, Helvetica, sans-serif;">
    <div style="margin-bottom: 20pt; margin-top: 20pt;font-size: 11pt;">
        Thanks and best regards<br>{{First Name}}
    </div>
    <div style="margin-bottom: 5px;">
        <table style="width:100%">
            <tr>
                <td colspan="1" style="border-top: 1px solid black;">
                </td>
            </tr>
        </table>
    </div>	
    <div style="float: left; margin-right: 20px;">
        <table style="border-collapse: collapse; margin: 0 auto; width: 500px; text-align: left;">
            <tr>
                <td style="width: 120px; vertical-align: middle; padding: 0 10px;">
                    <div style="display: flex; justify-content: flex-start; align-items: center; margin-bottom: 10px;">
                        <a href="https://www.mycompany.com"><img width=80% src="./logo.jpeg" alt="Comapny Logo"></a>
                    </div>
                </td>
                <td style="width: 250px; vertical-align: top; padding: 0px 5px;">
                    <div style="text-align: center;">
                        <div style="font-size: 110%;"><strong>{{First Name}} {{Last Name}}</strong></div>
                        <div style="font-size: 100%; margin-bottom: 1pt;">{{Title}}</div>
                        <div style="font-size: 80%; margin-bottom: 4pt;">{{Depatrment}}</div>
                        <div style="font-size: 80%; margin-bottom: 6pt;">{{Mobile}}</div>
                        <div style="font-size: 90%;"><a href="https://www.company.com">www.company.com</a></div>
                        <div style="font-size: 80%;">Phone +1 x769 x806 3066 {{Ext}}</div>
                        <div style="font-size: 80%;">Fax +1 7x69 2236 3627</div>
                    </div>
                </td>
            </tr>
        </table>
    </div>
    <div style="font-style: italic; font-size: 80%; margin-top: 10px; text-align: left; line-height: 1.7; width: 100%; display: inline-block;">
        <p>The content of this email is confidential and intended for the recipients 
            specified in this message only. It is strictly forbidden to share any 
            part of this message with any third party, without the agreement of the 
            originator of this email. If you received this message by mistake, 
            please forward this message to <a href="mailto:companyIT@comapny.com?subject=Received%20this%20email%20by%20mistake">companyIT@comapny.com</a> and delete it 
            afterwards, so that we can ensure such a mistake does not occur in the future.
            <br><br>Please care about our environment and think before you print, if you really want to use paper. We have only one earth.
        </p>
    </div>
</body>
</html>

2 responses to “Company-Wide Email Signatures on Windows Server”

  1. I have been browsing on-line more than 3 hours as of late, but I never
    discovered any attention-grabbing article like yours. It’s lovely worth sufficient
    for me. Personally, if all website owners and bloggers
    made good content as you probably did, the web
    will be much more helpful than ever before.

Leave a Reply

Your email address will not be published. Required fields are marked *