Entra / Microsoft 365 · Users & guests
Update outlook signature
Example script that builds and applies an Outlook desktop signature from Azure AD user properties and registry settings.
Connect & set up
Run these once per session. All scopes are read-only unless the script makes changes.
# Review required modules and connection steps before running.# Connect to Microsoft Graph or Exchange Online as needed for this script.
Run it
The main script. Copy it, or download the .ps1 and run it from your console.
$SignatureName = 'Office365ITPros'$LocalSignaturePath = (Get-Item env:appdata).Value + '\Microsoft\Signatures'$HtmlPath = $LocalSignaturePath + '\' + $SignatureName + '.htm'# Find the User Principal Name for the account as stored in the system registry$UserAccount = Get-ItemProperty -Path HKCU:\Software\Microsoft\Office\Outlook\Settings -Name Accounts | Select -ExpandProperty Accounts$UserId = (ConvertFrom-Json $UserAccount).UserUpn[0]# Retrieve the properties of the user from Azure Active Directory$UserProperties = Get-AzureADUser -ObjectId $UserId# Find Outlook Profiles in registry$CommonSettings = $False$Profiles = (Get-ChildItem HKCU:\Software\Microsoft\Office\16.0\Outlook\Profiles).PSChildName# This script can only deal with a single (default profile); more code needed to handle multiple profilesIf ($Profiles -eq $Null -or $Profiles.Count -ne 1) {Write-Host "Warning - Applying signature to all Outlook profiles"$OutlookProfilePath = "HKCU:\Software\Microsoft\\Office\16.0\Common\MailSettings"$CommonSettings = $True}Else { # Path to default profile is elsewhere in the registry$OutLookProfilePath = "HKCU:\Software\Microsoft\Office\16.0\Outlook\Profiles\" + $Profiles.Trim() + "\9375CFF0413111d3B88A00104B2A6676\00000001" }# If we have an Outlook profile, check that we can match the User Principal name with the account name that's stored in the registry for Outlook ProPlus# But only do this for now if we're not updating all profilesIf ($CommonSettings -eq False) {$OutlookProfile = Get-ItemProperty -Path $OutLookProfilePathIf ($OutlookProfile."Account Name" -ne $UserId) { # We don't have a profile matchWrite-Host "Can't match signature and Office 365 user principal name" ; exit }}Switch ($UserProperties.City) { # Make sure that users from the same location have the same address"Foxrock" {$City = "Dublin"; $StreetAddress = "Foxrock"; $PostalCode = "D18A52R2 Ireland" }"Frankfurt" {$City = "Frankfurt am Main"; $StreetAddress = "Freidrich-Ebert-Anlage 35-37"; $PostalCode = "D-60327 Germany"}"San Franciso" {$City = "San Francisco"; $StreetAddress = "14 Warren Street"; $PostalCode = "93404 United States of America"}Default {$City = "Dublin"; $StreetAddress = "Foxrock"; $PostalCode = "D18A52R2 Ireland" }}# Make sure we have a company nameIf ($Null -eq $UserProperties.CompanyName) { $CompanyName = "Office 365 for IT Pros"} Else { $CompanyName = $UserProperties.CompanyName }If ($Null -eq $UserProperties.Title) { $JobTitle = "Valued Employee" } Else { $JobTitle = $UserProperties.Title }# Construct a signature file in HTML format using the information fetched from Azure Active Directory$CompanyLogo = "https://i1.wp.com/office365itpros.com/wp-content/uploads/2020/02/2020EditionVerySmall.jpg"$HeadingLine = "<HTML><HEAD><TITLE>Signature</TITLE><BODY><BR><table style=`"FONT-SIZE: 8pt; COLOR: gray; FONT-FAMILY: `'Segoe UI`' `"> <tr>"$ImageLine = "<td ><img src='" + $CompanyLogo + "' border='0'></td>"$PersonLine = "<td padding='0'><B>" + $UserProperties.DisplayName + " </B> " + $JobTitle + "<BR>"$CompanyLine = "<b>" + $CompanyName + "</b> " + $StreetAddress + ", " + $City + ", " + $PostalCode + "<BR>" + $UserProperties.TelephoneNumber + "/" + $UserProperties.Mobile + " Email: " + $UserProperties.Mail + "<br><br>"# Facebook and Twitter icons$IconsLine = '<tr><td style="font-size: 10pt; font-family: Arial, sans-serif; padding-bottom: 0px; padding-top: 5px; padding-left: 10px; vertical-align: bottom;" valign="bottom"><span><a href="https://www.facebook.com/Office365itpros/" target="_blank" rel="noopener"><img border="0" width="23" alt="facebook icon" style="border:0; height:23px; width:23px" src="https://i0.wp.com/office365itpros.com/wp-content/uploads/2020/02/Facebook.png"></a> </span><span><a href="https://twitter.com/12Knocksinna" target="_blank" rel="noopener"><img border="0" width="23" alt="twitter icon" style="border:0; height:23px; width:23px" src="https://i1.wp.com/office365itpros.com/wp-content/uploads/2020/02/Twitter.png"></a></span></td></tr>'$EndLine = "</td></tr></table><BR><BR></BODY></HTML>"# Put everything together and output the HTML file$SignatureHTML = $HeadingLine + $ImageLine + $PersonLine + $CompanyLine + $Iconsline + $EndLine | Out-File $HtmlPath# Update the registry settings where Outlook picks up its signature informationIf (Test-Path $TargetForSignatures) {Get-Item -Path $OutlookProfilePath | New-Itemproperty -Name "New Signature" -value $SignatureName -Propertytype string -ForceGet-Item -Path $OutlookProfilePath | New-Itemproperty -Name "Reply-Forward Signature" -value $SignatureName -Propertytype string -Force }
Attribution
Author
Office365itpros