Skip to content

Instantly share code, notes, and snippets.

@sionta
Created July 31, 2023 09:57
Show Gist options
  • Save sionta/0c101a6fa9639d88ece2cad82fa74069 to your computer and use it in GitHub Desktop.
Save sionta/0c101a6fa9639d88ece2cad82fa74069 to your computer and use it in GitHub Desktop.
Fonts installer
#Requires -Version 3.0
<#
.SYNOPSIS
Fonts installer.
.DESCRIPTION
Install a specific font file or multiple from a directory.
.EXAMPLE
.\install-font.ps1 -Path .\myfonts\ -Recurse
Install all fonts including the 'myfonts' sub/directories.
PS > .\install-font.ps1 -Path .\dir\font-name.ttf
Only install specific file font 'font-name.ttf'.
.LINK
https://github.com/sionta/
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[Alias('p')][string]$Path,
[Alias('r')][switch]$Recurse
)
if (Test-Path $Path -PathType Leaf) {
foreach ($e in $('ttf', 'otf')) {
if (! $e.EndsWith("\.$e$")) {
Write-Host -ForegroundColor Red `
"ERROR: The '$Path' is not '.$e' format."
exit 1
}
}
}
try {
$files = Get-ChildItem $Path -Filter '*.?tf' -File -Recurse:$Recurse
}
catch {
throw $_
}
finally {
$shell = New-Object -ComObject Shell.Application
# $trash = $shell.NameSpace(0xa) # RecycleBin
$fonts = $shell.NameSpace(0x14)
$files = [Collections.Generic.List[System.IO.FileInfo]]::new()
$files.ForEach({ $files.Add($_) })
foreach ($file in $files) {
$objName = $file.Name
$objPath = $file.FullName
$usrFont = Test-Path "$env:LOCALAPPDATA/Microsoft/Windows/Fonts/$objName"
$sysFont = Test-Path "$env:SystemRoot/Fonts/$objName"
if ($usrFont -or $sysFont) {
Write-Warning "The '$objName' font already installed."
}
else {
$fonts.CopyHere($objPath)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment