Last active
January 29, 2024 19:21
-
-
Save asheroto/4391f22723e12dc8560a5bb99ff4ffb7 to your computer and use it in GitHub Desktop.
PowerShell function to install fonts on Windows. Supports both individual and bulk file installations. Offers wildcard and recursive directory searching. TTF and OTF fonts supported by default.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function Install-Font { | |
<# | |
.SYNOPSIS | |
Installs fonts from a specified path on Windows systems. | |
.DESCRIPTION | |
The Install-Font function supports handling individual font files, directories containing multiple fonts, and wildcard paths. It also supports recursive search for font files in the specified path and all its subdirectories. The function is capable of installing both TTF and OTF font types. | |
.PARAMETER Path | |
Specifies the path to the font file(s). This can be a path to an individual font file, a directory containing font files, or a wildcard path. The function accepts both relative and absolute paths. | |
.PARAMETER Recursive | |
When specified, the function will recursively search for font files in the specified path and all its subdirectories. This is useful for bulk installations from directories with nested subfolders. | |
.EXAMPLE | |
Install-Font -Path ".\MyFont.ttf" | |
This example installs a single font file named 'MyFont.ttf' located in the current directory. | |
.EXAMPLE | |
Install-Font -Path ".\MyFont.otf" | |
This example installs a single font file named 'MyFont.otf' located in the current directory. | |
.EXAMPLE | |
Install-Font -Path "C:\Users\User\Downloads\*.ttf" | |
This example installs all TTF fonts located in the 'Downloads' folder of the 'User' directory. | |
.EXAMPLE | |
Install-Font -Path "*.ttf" -Recursive | |
This example installs all TTF fonts found in the current directory and its subdirectories. | |
#> | |
param ( | |
[string]$Path, | |
[switch]$Recursive | |
) | |
# Get the path to the Fonts folder | |
$SystemFontsPath = [System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::Fonts) | |
# Find all font files based on the given path | |
$fontFiles = Get-ChildItem -Path $Path -Recurse:$Recursive -File | Where-Object { $_.Extension -eq '.ttf' -or $_.Extension -eq '.otf' } | |
foreach ($file in $fontFiles) { | |
Write-Output "Installing $($file.Name)" | |
# Construct the path to the font file in the Fonts folder | |
$FontDestination = Join-Path -Path $SystemFontsPath -ChildPath $file.Name | |
# Get font name from font file | |
$ShellFolder = (New-Object -COMObject Shell.Application).Namespace($file.DirectoryName) | |
$ShellFile = $ShellFolder.ParseName($file.Name) | |
$FontType = $ShellFolder.GetDetailsOf($ShellFile, 2) | |
$FontName = $ShellFolder.GetDetailsOf($ShellFile, 21) | |
# Check if the file is a font file | |
If (-not ($FontType -Like '*font*')) { | |
Write-Output " $($file.Name) is not a recognized font file" | |
Continue | |
} | |
# If the font file doesn't exist in the Fonts folder | |
if (-not (Test-Path -Path $FontDestination)) { | |
# Copy the font file to the Fonts folder | |
Copy-Item -Path $file.FullName -Destination $FontDestination | |
# Register the font in the registry for persistence | |
$registryPath = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts" | |
if ($null -ne $FontName) { | |
$null = Set-ItemProperty -Path $registryPath -Name "$FontName (TrueType)" -Value $file.Name | |
} | |
Write-Output " Installed `"$FontName``" | |
} else { | |
Write-Output " `"$FontName`" is already installed" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment