Free Office 365 email signatures with PowerShell

There are plenty of paid options out there to manage Office 365 email signatures, but what if you could do it for free?… PowerShell to the rescue! This post is collation and adaptation of what I’ve found online when implementing staff signatures in my environment. Note: the focus here will be on applying signatures in the Outlook Web App, although these signatures can be deployed to Outlook desktop applications when placed in the right userprofile folder etc. Might do an update down the track.

Build signatures

The first part of this script builds an .htm file for each user’s signature. AD users are specified by OU in this example; alternatively, the script could be modified to search a group. The specific AD user attributes used in the script will depend upon which fields info is stored in. Reference here or here for user attribute reference information that you can substitute as required.

The .htm file is built using the defined attributes for each user. The HTML code can be modified to suit your specific design requirements.

Push signatures to Office 365

The second part pushes the generated signatures to Office 365. In this instance, the password for the service user is stored in a text file. In reality, the connection to Exchange Online can be initiated however you like. The script will then search the save location for the signature files, and attempt to set the signature for each file/user present.

The code below – once modified for your environment – can be run as a Scheduled Task to update signatures as required. Your logging functionality of choice can also be added. Feel free to comment/Tweet your feedback. Enjoy!

#set folder location for files, the folder must already exist
$save_location = 'c:\folder'
$email_domain = '@my.domain'

#--------------------------------------------------------------------
#BUILD SIGNATURE FILES
Import-Module ActiveDirectory

#OU to search for users
$users = Get-ADUser -filter * -searchbase "OU=staff,DC=my,DC=domain" -Properties *

#retrieve and set whatever AD properties or other variables you require
foreach ($user in $users) {
 $full_name = Get-ADUser $user -Properties personalTitle | % { "$($_.personalTitle) $($_.Name)" } #using personalTitle to hold user salutation, ie Mrs., Mr., etc.
 $account_name = "$($User.sAMAccountName)"
 $job_title = "$($User.description)"
 $company = "$($User.office)"
 $tagline = "we're a great school" #your tagline here
 $web = 'mysite.com' #your site here
 $phone = '123456798' #your number here
 $address = '1 Organisation Way
City, STATE 1234' #your address here $banner = "google.com/logo.png" #your banner image file here $bannerlink = "https://google.com/" #your banner's clickable link here $disclaimer = "CAUTION..." #your disclaimer here #construct and write the html signature file $output_file = $save_location + $account_name + ".htm" LogWrite "Now attempting to create signature html file for $full_name" #customise the HTML below to create your desired layout "" + $full_name + "
" + $job_title + "

" + $company + "
" + $tagline + "
", $address + "
w: " + $web + " | p: ", $phone +"
", "

`"email

" + $disclaimer + "
" | Out-File -Encoding utf8 $output_file } #-------------------------------------------------------------------- #PUSH SIGNATURES TO OFFICE 365 #connect to O365 tenant $MSEOLadminuser = "
[email protected]" $MSEOLPasswordFile = ("C:\path\$MSEOLadminuser-ExchangeOnline.txt") if (!(Test-Path -Path $MSEOLPasswordFile)) { LogWrite "You don't have an admin password assigned for $MSEOLadminuser, please provide the password followed with enter below:" Read-Host -assecurestring | convertfrom-securestring | out-file $MSEOLpasswordfile } $MSEOLpassword = get-content $MSEOLPasswordFile | convertto-securestring -ErrorAction Stop $MSEOLcredentials = new-object -typename System.Management.Automation.PSCredential -argumentlist $MSEOLadminuser,$MSEOLpassword -ErrorAction Stop $MSEOLSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -credential $MSEOLcredentials -Authentication Basic -AllowRedirection -ErrorAction Stop Import-PSSession $MSEOLSession -ErrorAction Stop #Get a list of all the filenames in the target folder $sig_files = Get-ChildItem -Path $save_location #Now push the html to the users signature foreach ($item in $sig_files) { $user_name = $($item.Basename) + $email_domain $filename = $save_location + $($item.Basename) + ".htm" LogWrite "Now attempting to set signature for $user_name" set-mailboxmessageconfiguration -identity $user_name -signaturehtml (get-content $filename) -autoaddsignature $true } #disconnect O365 connection Remove-PSSession $MSEOLSession