Skip to content

Instantly share code, notes, and snippets.

@justaguywhocodes
Created June 10, 2025 13:20
Show Gist options
  • Save justaguywhocodes/23347cd42486b7f2df37e630a1f4f594 to your computer and use it in GitHub Desktop.
Save justaguywhocodes/23347cd42486b7f2df37e630a1f4f594 to your computer and use it in GitHub Desktop.
# Parameters
param (
[Parameter(Mandatory=$true)]
[string]$FilePath,
[Parameter(Mandatory=$true)]
[string]$OriginalFont,
[Parameter(Mandatory=$true)]
[string]$NewFont
)
# Function to validate file path and extension
function Test-PowerPointFile {
param ([string]$Path)
if (-not (Test-Path $Path)) {
Write-Error "File not found: $Path"
return $false
}
if (-not ($Path -match "\.pptx?$")) {
Write-Error "File must be a PowerPoint file (.ppt or .pptx)"
return $false
}
return $true
}
try {
# Validate input file
if (-not (Test-PowerPointFile -Path $FilePath)) {
exit 1
}
# Create PowerPoint application object
$powerPoint = New-Object -ComObject PowerPoint.Application
# Open the presentation
$presentation = $powerPoint.Presentations.Open($FilePath)
# Counter for changes
$changes = 0
# Loop through each slide
foreach ($slide in $presentation.Slides) {
# Loop through each shape in the slide
foreach ($shape in $slide.Shapes) {
# Check if shape has text
if ($shape.HasTextFrame -eq -1) { # -1 = msoTrue
$textRange = $shape.TextFrame.TextRange
# Check if the font matches the original font
if ($textRange.Font.Name -eq $OriginalFont) {
# Change the font
$textRange.Font.Name = $NewFont
$changes++
}
}
}
}
# Save and close the presentation
$presentation.Save()
$presentation.Close()
# Quit PowerPoint
$powerPoint.Quit()
# Release COM objects
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($presentation) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($powerPoint) | Out-Null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
Write-Host "Font replacement complete. Changed $changes instances of $OriginalFont to $NewFont"
}
catch {
Write-Error "An error occurred: $_"
# Ensure PowerPoint is closed in case of error
if ($presentation) {
$presentation.Close()
}
if ($powerPoint) {
$powerPoint.Quit()
}
# Release COM objects
if ($presentation) {
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($presentation) | Out-Null
}
if ($powerPoint) {
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($powerPoint) | Out-Null
}
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
exit 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment