Skip to content

Instantly share code, notes, and snippets.

@wise-io
Last active February 17, 2022 02:48
Show Gist options
  • Save wise-io/33d424b5c9b766eb45d1a70c170376e2 to your computer and use it in GitHub Desktop.
Save wise-io/33d424b5c9b766eb45d1a70c170376e2 to your computer and use it in GitHub Desktop.
PowerShell Script to Install Custom Fonts using FontReg
# Downloads and installs fonts from a zip archive
param (
[Parameter(Mandatory = $true)]
[ValidatePattern( '^(https)://' )]
[System.Uri]$URL
)
$FontRegURL = '' # Add download link to FontReg here
$FontReg = "$env:temp\FontReg.exe"
$Archive = "$env:temp\Fonts.zip"
$Fonts = "$env:temp\Fonts"
$LocalFontStore = ($env:SystemRoot + '\Fonts\')
try {
# Validate URL file extension
if (!(([System.IO.Path]::GetExtension($URL.ToString())) -eq '.zip')) {
Write-Output 'Provided url did not link to a zip archive.'
exit
}
# Download necessary files & extract font archive
Write-Output 'Downloading font archive...'
Invoke-WebRequest -Uri $FontRegURL -OutFile $FontReg
Invoke-WebRequest -Uri $URL -OutFile $Archive
Expand-Archive -Path $Archive -DestinationPath $Fonts -Force
# Verify archive contains supported fonts
if ((Get-ChildItem -Path $Fonts | Where-Object { $_.extension -in '.ttf', '.ttc', '.otf' } | Measure-Object).Count -ne 0 ) {
# Copy fonts to local font store
Get-ChildItem $Fonts | Where-Object { $_.extension -in '.ttf', '.ttc', '.otf' } | Copy-Item -Destination $LocalFontStore -Force
# Register fonts
Write-Output 'Registering fonts...'
Start-Process -Wait -FilePath $FontReg
}
else {
Write-Output 'Archive did not contain supported font files (.ttf, .ttc, or .otf).'
}
}
catch { throw $Error }
finally {
# Clean up temp files
Write-Output 'Cleaning up downloaded files...'
Remove-Item $Archive, $Fonts, $FontReg -Recurse
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment