Last active
July 11, 2024 04:35
-
-
Save vsnthdev/18cfd62fd25149873de578f7f213cb6c to your computer and use it in GitHub Desktop.
Install Google Fonts through Windows Powershell
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
# | |
# 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
I don't have a Windows machine at the moment, otherwise I'd have got it fixed.