Powershell Outlook Signature Local AD

powershell
<#
.SYNOPSIS
    Generate Outlook Signature with data from Local AD
.DESCRIPTION
    This script generates Outlook signatures automatically using Active Directory data.
    It should be run as a logon script. For testing purposes, you can run it with initials.
.NOTES
    Author: Kasper Lundgaard Christensen - TODO-IT
    Date: 01-04-2024
    Version: 1.0
#>

# Parameters
param(
    [string]$UserOverwrite
)

# Getting Active Directory information for current user
$user = (([adsisearcher]"(&(objectCategory=User)(samaccountname=$env:username))").FindOne().Properties)

# Overwrite user for testing purposes
if ($UserOverwrite) {
    $user = (([adsisearcher]"(&(objectCategory=User)(samaccountname=$UserOverwrite))").FindOne().Properties)
    Write-Host "Testing with user: $UserOverwrite"
}

# If the user is not found in Active Directory exit the script
if (!$user) {
    Write-Error "User not found in Active Directory"
    exit 1
}

# Signature configuration
$folderLocation = $Env:APPDATA + '\Microsoft\Signatures'
$filename = "AD Signature"
$file = "$folderLocation\$filename"

# Company information
$companyName = "Company Name"
$logo = "Logo link"
$logoURL = "Logo URL"
$companyPhone = "Phone Number"
$slogan_1 = "Slogan"
$considerPrint = "Please consider the environment before printing this email"

# Create signatures folder if it doesn't exist
if (!(Test-Path -Path $folderLocation)) {
    New-Item -ItemType Directory -Path $folderLocation -Force
}

# Get user properties from Active Directory
$userProperties = @{
    DisplayName  = if ($user.name.Count -gt 0) { $user.name[0] } else { $null }
    JobTitle     = if ($user.title.Count -gt 0) { $user.title[0] } else { $null }
    Email        = if ($user.mail.Count -gt 0) { $user.mail[0] } else { $null }
    Company      = if ($user.company.Count -gt 0) { $user.company[0] } else { $null }
    Mobile       = if ($user.mobile.Count -gt 0) { $user.mobile[0] } else { $null }
    DirectDial   = if ($user.homephone.Count -gt 0) { $user.homephone[0] } else { $null }
    Telephone    = if ($user.telephonenumber.Count -gt 0) { $user.telephonenumber[0] } else { $null }
    Website      = if ($user.wwwhomepage.Count -gt 0) { $user.wwwhomepage[0] } else { $null }
    PoBox        = if ($user.postofficebox.Count -gt 0) { $user.postofficebox[0] } else { $null }
    Office       = if ($user.physicaldeliveryofficename.Count -gt 0) { $user.physicaldeliveryofficename[0] } else { $null }
    Street       = if ($user.streetaddress.Count -gt 0) { $user.streetaddress[0] } else { $null }
    City         = if ($user.l.Count -gt 0) { $user.l[0] } else { $null }
    State        = if ($user.st.Count -gt 0) { $user.st[0] } else { $null }
    ZipCode      = if ($user.postalcode.Count -gt 0) { $user.postalcode[0] } else { $null }
}

# Group Check for custom logo
$Group = [ADSI]"LDAP://CN=Signature_CompanyLogo,OU=Signature,OU=Security Groups,OU=Users,DC=contoso,DC=dk"
$Group.Member | ForEach-Object {
    if ($user.distinguishedname -match $_) {
        $logo = "Alternative Logo Link"
    }
}

# CSS Styles
$style = @"
<style>
    p, table, td, tr, a, span {
        font-family: Calibri, Helvetica, sans-serif;
        font-size: 11pt;
        color: #000000;
    }

    span.blue {
        color: #000000;
    }

    table {
        margin: 0;
        padding: 0;
    }

    a {
        text-decoration: none;
    }

    hr {
        border: none;
        height: 1px;
        background-color: #000000;
        color: #000000;
        width: 700px;
    }

    .Contact {
        width: 150px;
    }

    table.main {
        /* Main table styles */
    }
</style>
"@

# HTML Signature
$htmlSignature = @"
<span>Med venlig hilsen/Kind regards/Mit freundlichen Grüssen</span><br />
$(if ($userProperties.DisplayName) { "<span><b>$($userProperties.DisplayName)</b></span><br />" })
$(if ($userProperties.JobTitle) { "<span>$($userProperties.JobTitle)</span><br /><br />" })

<p>
<table class='main'>
    <tr>
        <td colspan='2' style='padding-right: 75px;'>
            $(if ($logo) { "<a href='$logoURL'><img src='$logo' /></a>" })
        </td>
    </tr>
    <tr>
        <table>
            $(if ($userProperties.Telephone) { "<tr><td class='Contact'>Hovednummer: </td><td><a href='tel:$companyPhone'>$companyPhone</a></td></tr>" })
            $(if ($userProperties.Mobile) { "<tr><td class='Contact'>Mobil: </td><td><a href='tel:+45 $($userProperties.Mobile)'>+45 $($userProperties.Mobile)</a></td></tr>" })
            $(if ($userProperties.Email) { "<tr><td class='Contact'>Email: </td><td><a href='mailto:$($userProperties.Email)'>$($userProperties.Email)</a></td></tr>" })
            $(if ($userProperties.Website) { "<tr><td class='Contact'>Website: </td><td><a href='https://$($userProperties.Website)'>$($userProperties.Website)</a></td></tr>" })
        </table>
        <table>
            <tr>
                <br/>
                <br/>
                <br/>
            </tr>
            <tr>
                <td colspan='2' style='padding-bottom: 10px;'>
                    $(if ($companyName) { "<b>$companyName</b><br/>" })
                    $(if ($userProperties.Street) { "$($userProperties.Street)<br/>" })
                    $(if ($userProperties.ZipCode) { "$($userProperties.ZipCode) " })
                    $(if ($userProperties.City) { $userProperties.City })
                </td>
            </tr>
            <tr>
                <td colspan='2'>
                    <p>$slogan_1</p>
                </td>
            </tr>
        </table>
    </tr>
</table>
</p>
<br />
"@

# Save HTML signature
$style + $htmlSignature | Out-File "$file.htm" -Encoding UTF8

# Text signature for plain text emails
$textSignature = @"
Med venlig hilsen/Kind regards/Mit freundlichen Grüssen
$(if ($userProperties.DisplayName) { $userProperties.DisplayName })
$(if ($userProperties.JobTitle) { $userProperties.JobTitle })

$(if ($userProperties.Telephone) { "Hovednummer: $companyPhone" })
$(if ($userProperties.Mobile) { "Mobil: +45 $($userProperties.Mobile)" })
$(if ($userProperties.Email) { "Email: $($userProperties.Email)" })

$(if ($companyName) { $companyName })
$(if ($userProperties.Street) { $userProperties.Street })
$(if ($userProperties.ZipCode) { $userProperties.ZipCode }) $(if ($userProperties.City) { $userProperties.City })
"@

# Save text signature
$textSignature | Out-File "$file.txt" -Encoding Default

# Configure Outlook signatures in registry
$outlookVersions = @(
    @{ Version = "16.0"; Path = "HKCU:\Software\Microsoft\Office\16.0" },
    @{ Version = "14.0"; Path = "HKCU:\Software\Microsoft\Office\14.0" }
)

foreach ($outlook in $outlookVersions) {
    $commonPath = $outlook.Path + "\Common"
    
    if (Test-Path $commonPath) {
        # Set signature path
        if (Test-Path "$commonPath\General") {
            Set-ItemProperty -Path "$commonPath\General" -Name "Signatures" -Value "Signatures" -Force
        }
        
        # Set default signatures
        if (Test-Path "$commonPath\MailSettings") {
            Set-ItemProperty -Path "$commonPath\MailSettings" -Name "NewSignature" -Value $filename -Force
            Set-ItemProperty -Path "$commonPath\MailSettings" -Name "ReplySignature" -Value $filename -Force
        }
        
        # Remove First-Run flag
        $setupPath = $outlook.Path + "\Outlook\Setup"
        if (Test-Path $setupPath) {
            Remove-ItemProperty -Path $setupPath -Name "First-Run" -ErrorAction SilentlyContinue
        }
    }
}

Write-Host "Signature generated successfully for $($userProperties.DisplayName)"

Brugsanvisning:

Normal brug (som logonscript):

  • Deploy scriptet som logonscript via Group Policy

Test kørsel:

powershell
.\Script.ps1 -UserOverwrite "Initialer"

Funktioner:

  • Henter brugerinformation automatisk fra Active Directory

  • Genererer både HTML og tekst-version af signaturen

  • Understøtter flere Outlook-versioner (2010, 2016, Office365)

  • Mulighed for at teste med specifikke brugere

  • Dynamisk logo baseret på AD-gruppemedlemskab

  • Automatisk opsætning af Outlook-signatur

Bemærk: Husk at tilpasse AD-stier, company information og logo-stier til din organisation før brug.