Skip to content

Instantly share code, notes, and snippets.

@vsnthdev
Last active September 26, 2022 20:24
Show Gist options
  • Save vsnthdev/18cfd62fd25149873de578f7f213cb6c to your computer and use it in GitHub Desktop.
Save vsnthdev/18cfd62fd25149873de578f7f213cb6c to your computer and use it in GitHub Desktop.
Install Google Fonts through Windows Powershell
#
# Installs Google Fonts as system fonts using Windows Powershell
# Created On 20 September 2021
#
# Execute: '$Font = "Open Sans"', (iwr https://git.io/JzcAW).Content | powershell -c -
# Execute locally: '$Font = "Open Sans"', (Get-Content ./install.ps1) | powershell -c -
# ensure that we're running as Administrator
if ('S-1-5-32-544' -notin [System.Security.Principal.WindowsIdentity]::GetCurrent().Groups) {
Write-Output "Please run as administrator."
Exit
}
# get my Twitter username's absolute URL
$TwitterRedirect = Invoke-WebRequest -UseBasicParsing -Method Get -Uri "https://vas.cx/twitter"
if ($null -ne $TwitterRedirect.BaseResponse.ResponseUri) {
# This is for Powershell 5
$TwitterURL = $TwitterRedirect.BaseResponse.ResponseUri.AbsoluteUri
}
elseif ($null -ne $TwitterRedirect.BaseResponse.RequestMessage.RequestUri) {
# This is for Powershell core
$TwitterURL = $TwitterRedirect.BaseResponse.RequestMessage.RequestUri.AbsoluteUri
}
$TwitterUsername = $TwitterURL.substring(20)
# variables we'll be using
$URL = "https://fonts.google.com/download?family=$Font"
$ArchiveDest = "$env:TEMP\vsnthdev_google_font_installer"
# create the temp folder
mkdir "$ArchiveDest" -ErrorAction SilentlyContinue | Out-Null
# download the font archive from Google
$global:ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest -UseBasicParsing -Uri "$URL" -OutFile "$ArchiveDest\font.zip" | Out-Null
# extract the downloaded archive
Expand-Archive -Path "$ArchiveDest\font.zip" -DestinationPath "$ArchiveDest" -Force
# grab all the font files in the fonts folder
$Fonts = Get-ChildItem -Path $ArchiveDest -Include ('*.fon', '*.otf', '*.ttc', '*.ttf') -Recurse
# loop through each font and install it
foreach ($Font in $Fonts) {
try {
Copy-Item $Font "C:\Windows\Fonts"
New-ItemProperty -Name $Font.BaseName -Path "HKLM:\Software\Microsoft\Windows NT\CurrentVersion\Fonts" -PropertyType string -Value $Font.name -Force | Out-Null
}
catch {
Write-Output "Failed to install ""${FontFamily}"""
Write-Output "Ensure that:"
Write-Output " 1. Font exists on Google Fonts."
Write-Output " 1. The font is already installed."
Write-Output " 1. Powershell is running as an administrator."
Write-Output ""
Write-Output "If it still doesn't work, shoot me a tweet @$TwitterUsername"
Write-Output ""
Exit
}
}
# delete the fonts folder
Remove-Item -Path "$ArchiveDest" -Recurse -Force | Out-Null
# tell the user
Write-Output "Installed ""${Font}"""
Write-Output "Follow me on $TwitterURL"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment