Outlook Auto Signature

This script will extract information from Active Directory, format the text and set it as the default signature in Outlook.

Hopefully the notes in the script will show what needs to be modified, if you need help send me an email.

It is a heavily modified version for the script from Captain Qwerty at EduGeek.

Both HTML and TXT signatures are configured and set as the default.

Image, such as company logos, can also be added.


I added a Group Policy object as a logon script to run the following script to launch the Powershell script.

powershell.exe -executionpolicy bypass -windowstyle hidden -noninteractive -nologo -file "\\server\Share\OutlookSignature.ps1"

The # Group Check section will look to see if the user is in the nominated group. If they are, the group email address will also be added to the signature. Good for use in areas such as Human Resources where a group email is typically used.


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

if($user) {
  # Create the signatures folder and sets the name of the signature file
  $folderlocation = $Env:appdata + '\\Microsoft\\signatures'
  $filename = "Signature"
  $file  = "$folderLocation\\$filename"

  if(!(Test-Path -Path $folderlocation )){
      New-Item -ItemType directory -Path $folderlocation
  }

  # Company name and logo
  $companyName = "Castlemaine Health"
  $logo = "\\server\share\OutlookSig\EmailImage.jpg"
  $logo2 = "\\server\share\OutlookSig\PeopleMatterLogo.JPG"

  # Get the users properties (These should always be in Active Directory and Unique)
  if($user.name.count -gt 0){$displayName = $user.name[0]}
  if($user.department.count -gt 0){$department = $user.department[0]}
  if($user.title.count -gt 0){$jobTitle = $user.title[0]}
  if($user.homephone.count -gt 0){$directDial = $user.homephone[0]}
  if($user.mobile.count -gt 0){$mobileNumber = $user.mobile[0]}
  if($user.facsimiletelephonenumber.count -gt 0){$faxnumber = $user.facsimiletelephonenumber[0]}
  if($user.mail.count -gt 0){$email = $user.mail[0]}
  if($user.wwwhomepage.count -gt 0){$website = $user.wwwhomepage[0]}

  # Address
  if($user.postofficebox.count -gt 0){$poBox = $user.postofficebox[0]}
  if($user.streetaddress.count -gt 0){$street = $user.streetaddress[0]}
  if($user.l.count -gt 0){$city = $user.l[0]}
  if($user.st.count -gt 0){$state = $user.st[0]}
  if($user.postalcode.count -gt 0){$zipCode = $user.postalcode[0]}
  if($user.telephonenumber.count -gt 0){$telephone = $user.telephonenumber[0]}
  if($user.physicaldeliveryofficename.count -gt 0){$office = $user.physicaldeliveryofficename}

  # Group Check
  $Group = [ADSI]"LDAP://cn=IT Alerts,OU=Groups,OU=+++,DC====,DC=vic,DC=gov,DC=au"
  $Group.Member | ForEach-Object {if ($user.distinguishedname -match $_) {$ItStaff = $true} }

  $Group = [ADSI]"LDAP://cn=Human Resources,OU=Groups,OU=+++,DC====,DC=vic,DC=gov,DC=au"
  $Group.Member | ForEach-Object {if ($user.distinguishedname -match $_) {$HRStaff = $true} }

  $Group = [ADSI]"LDAP://cn=Department Heads,OU=Groups,OU=+++,DC====,DC=vic,DC=gov,DC=au"
  $Group.Member | ForEach-Object {if ($user.distinguishedname -match $_) {$PMLogo = $true} }

  $Group = [ADSI]"LDAP://cn=Payroll,OU=Groups,OU=+++,DC====,DC=vic,DC=gov,DC=au"
  $Group.Member | ForEach-Object {if ($user.distinguishedname -match $_) {$Payroll = $true} }

# Building Style Sheet
  $style = 
  @"
  <style>
  p, table, td, tr, a, span { 
      font-family: DIN, Arial, Helvetica, sans-serif;
      font-size:  12pt;
      color: #132647;
  }

  span.blue
  {
      color: #28b8ce;
  }

  table {
      margin: 0;
      padding: 0;
  }

  a { 
  text-decoration: none;
  }

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

  table.main {
      border-top: 1px solid #132467;
  }
  </style>
"@

  # Building HTML
  $signature = 
  @"
  <p>
    $(if($displayName){ $displayName+" |"})
    $(if($jobTitle){ $jobTitle+" |"})
    $(if($department){ $department+", "} $companyName+" |")
    $(if($street){ $street+", " })
    $(if($city){ $city+", " })
    $(if($state){ $state+", " })
    $(if($zipCode){ $zipCode})
    <br>
    $(if($telephone){"P: $($telephone)"+" |"})
    $(if($mobileNumber){"M: $($mobileNumber)"+" |"})
    $(if($faxnumber){"F: $($faxnumber)"+" |"})
    $(if($ITStaff){"<a href='mailto:itdept@domain' >IT Helpdesk</a>"+" |"})
    $(if($HRStaff){"<a href='mailto:hr@domain' >Human Resources</a>"+" |"})
    $(if($Payroll){"<a href='mailto:payoffice@domain' >Pay Office</a>"+" |"})
    $(if($website){"<a href='https://$website' >$($website)</a>"})
    <br>
    <br>
    $(if($PMLogo){"<img src='$logo2' /> <br>"})
    $(if($logo){"<img src='$logo' />"})
  <br><br>

  </p>
  <br/>
"@

  # Save the HTML to the signature file
  $style + $signature | out-file "$file.htm" -encoding ascii

# Build the txt version for none rich text emails
  $signature = 
  @"
  $(if($displayName){ $displayName }) | $(if($jobTitle){ $jobTitle }) | $(if($companyName){ $companyName }) | $(if($street){ $street+", " })$(if($city){ $city+", " })$(if($state){ $state+", " })$(if($zipCode){ $zipCode })
  $(if($telephone){"T: "+$telephone+"| "})$(if($faxnumber){"F: "+$faxnumber+"| "})$(if($mobileNumber){"M: "+$mobileNumber+"| "})$(if($website){"W: "+$website})
  $(if($HRStaff){"Contact Human Resources - hr@domain"})$(if($ITStaff){"Contact the IT Helpdesk - itdept@domain"})$(if($Payroll){"Contact the Pay Office - payoffice@domain"})


"@

  # Output the text to the signatures folder
  $signature | out-file "$file.txt" -encoding ascii

  # Setting the regkeys for Outlook 2016
  if (test-path "HKCU:\\Software\\Microsoft\\Office\\16.0\\Common\\General") 
  {
      get-item -path HKCU:\\Software\\Microsoft\\Office\\16.0\\Common\\General | new-Itemproperty -name Signatures -value signatures -propertytype string -force
      get-item -path HKCU:\\Software\\Microsoft\\Office\\16.0\\Common\\MailSettings | new-Itemproperty -name NewSignature -value $filename -propertytype string -force
      get-item -path HKCU:\\Software\\Microsoft\\Office\\16.0\\Common\\MailSettings | new-Itemproperty -name ReplySignature -value $filename -propertytype string -force
      Remove-ItemProperty -Path HKCU:\\Software\\Microsoft\\Office\\16.0\\Outlook\\Setup -Name "First-Run" -ErrorAction silentlycontinue
  }


  if (Test-Path "HKCU:\\SOFTWARE\\Microsoft\\Office\\16.0\\Outlook\\Options\\Mail") 
  {
      get-Item -Path HKCU:\\SOFTWARE\\Microsoft\\Office\\16.0\\Outlook\\Options\\Mail | new-Itemproperty -Name "Send Pictures With Document" -Value 1 -PropertyType DWORD -Force
  }
  ELSE
  {
     new-item -Path HKCU:\\SOFTWARE\\Microsoft\\Office\\16.0\\Outlook\\Options\\Mail -force
     new-itemproperty -Path HKCU:\\SOFTWARE\\Microsoft\\Office\\16.0\\Outlook\\Options\\Mail -Name "Send Pictures With Document" -Value 1 -PropertyType DWORD -Force
  }

}