Signature Profile script - Knowledgebase - CLOUDRAIN

Signature Profile script Print

  • Signature Profile script
  • 0

Signature Profile script

 

###############################################################################
# CLOUDRAIN – OUTLOOK SIGNATURE AUTO-INSTALL SCRIPT (MS GRAPH)
# Uses Microsoft Graph for user data + browser login
###############################################################################

# ---------------------------------------------
# LOAD MICROSOFT GRAPH MODULE
# ---------------------------------------------
Import-Module Microsoft.Graph.Users -ErrorAction Stop

# ---------------------------------------------
# LOGIN TO MICROSOFT GRAPH
# ---------------------------------------------
Write-Host "Please sign in with your Microsoft 365 account..." -ForegroundColor Cyan

Connect-MgGraph -Scopes "User.Read","User.ReadBasic.All" -ErrorAction Stop

# ---------------------------------------------
# GET USER DETAILS FROM MS GRAPH (RELIABLE METHOD)
# ---------------------------------------------
Write-Host "Getting user info from Microsoft Graph…" -ForegroundColor Cyan

# Retrieve Graph session context
$Context = Get-MgContext
$UserUPN = $Context.Account

if (-not $UserUPN) {
Write-Host "ERROR: No logged-in Graph user detected." -ForegroundColor Red
Write-Host "Run Connect-MgGraph manually before running this script."
exit
}

# Retrieve user profile from Microsoft Graph
$User = Get-MgUser -UserId $UserUPN `
-Property DisplayName,JobTitle,Mail,MobilePhone

$DisplayName = $User.DisplayName
$Title = $User.JobTitle
$Email = $User.Mail
$MobilePhone = $User.MobilePhone

# Set blanks if missing
if (-not $Title) { $Title = "" }
if (-not $MobilePhone) { $MobilePhone = "" }

Write-Host "User: $DisplayName" -ForegroundColor Green
Write-Host "Job title: $Title"
Write-Host "Email: $Email"
Write-Host "Mobile phone: $MobilePhone"

# ---------------------------------------------
# SIGNATURE FILE PATHS
# ---------------------------------------------
$SigName = "CompanySignature"
$SourcePath = "C:\Users\Administrator\Desktop\Signatures"
$OutlookSigPath = "$env:APPDATA\Microsoft\Signatures"

if (!(Test-Path $OutlookSigPath)) {
New-Item -ItemType Directory -Path $OutlookSigPath | Out-Null
}

$Files = @("$SigName.htm", "$SigName.txt", "$SigName.rtf")

# ---------------------------------------------
# REPLACE PLACEHOLDERS IN SIGNATURE FILES
# ---------------------------------------------
foreach ($File in $Files) {
$SourceFile = Join-Path $SourcePath $File
$DestFile = Join-Path $OutlookSigPath $File

if (Test-Path $SourceFile) {
$Content = Get-Content $SourceFile -Raw

$Content = $Content.Replace("%%DisplayName%%", $DisplayName)
$Content = $Content.Replace("%%Job title%%", $Title)
$Content = $Content.Replace("%%Email%%", $Email)
$Content = $Content.Replace("%%Mobile phone%%", $MobilePhone)

Set-Content -Path $DestFile -Value $Content -Force
}
}

Write-Host "Signature files updated successfully." -ForegroundColor Green

# ---------------------------------------------
# SET DEFAULT SIGNATURE IN OUTLOOK
# ---------------------------------------------
$RegPath = "HKCU:\Software\Microsoft\Office\16.0\Common\MailSettings"

If (!(Test-Path $RegPath)) {
New-Item -Path $RegPath -Force | Out-Null
}

 

Write-Host "Outlook signature configured successfully." -ForegroundColor Green

# ---------------------------------------------
# DISCONNECT GRAPH SESSION
# ---------------------------------------------
Disconnect-MgGraph


Was this answer helpful?

« Back

Powered by WHMCompleteSolution