Skip to content

Instantly share code, notes, and snippets.

@nathanverrilli
Last active January 26, 2024 15:43
Show Gist options
  • Save nathanverrilli/d3f12fbad87953e28aae78b5f191cdab to your computer and use it in GitHub Desktop.
Save nathanverrilli/d3f12fbad87953e28aae78b5f191cdab to your computer and use it in GitHub Desktop.
Add a font to the list of permissible console fonts for windows powershell terminal // painlessly edit the relevant registry key
param (
[string]$FontName,
[string]$FontFile,
[switch]$Help
);
function isAdminMode() {
return ([Security.Principal.WindowsPrincipal] `
[Security.Principal.WindowsIdentity]::GetCurrent() `
).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
}
####################################
# NULL comes in a number of shapes and forms in powershell
function isNull($obj) {
if ( ($obj -eq $null) -or
(($obj -is [String]) -and ($obj -eq [String]::Empty)) -or
($obj -is [DBNull]) -or
($obj -is [System.Management.Automation.Language.NullString]) )
{ return $true; }
return $false;
}
####################################
function printHelp {
write-host " "
write-host " Usage: $scriptName -FontName <fontname> -FontFile <truetypefontfile>"
write-host " add font <fontname> in <truetypefontfile> to console fonts registry key"
write-host " <truetypefontfile> must be present in system fonts directory"
write-host " Please note that only truetype (.ttf) fonts may be installed to the powershell terminal"
write-host " Please note that this script must be run with elevated permissions"
write-host " "
write-host " Usage: $scriptName -Help"
write-host " print this message"
write-host " "
write-host " Example: ./$scriptName 'Zen Mono Regular' -FontFile zenmono-r-6.1.3.ttg"
write-host " "
exit 0;
}
####################################
#the script name might be 'InstallConsoleFont' but it might have been changed, too
$scriptName = $MyInvocation.MyCommand.Name
$adminMode = isAdminMode
if ( ($Help) -or
(isNull($FontName)) -or
(isNull($FontFile)) -or
(-not $adminMode ) ) {
printHelp
}
$key = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Console\TrueTypeFont"
## Find out if $FontName is installed on the system
$fontFileName = join-path $env:windir "Fonts\$FontFile"
if(!(test-path $fontFileName))
{
write-host "File $fontFileName is not present."
write-host "Please check that the desired font is installed in a .TTF format"
printHelp
break
}
## Determine if $FontName is already installed as a command window font
$installed = get-itemproperty $key |
get-member |
where-object { $_.Name -match "^0+$" } |
where-object { $_.Definition -match "$FontName" }
if($installed -ne $null)
{
write-host "The $FontName font is already installed as a command window font"
write-host "or another font is installed with that name ( $FontName )"
write-host " "
break
}
## Find out what the largest string of zeros is
$zeros = (get-itemproperty $key |
get-member |
where-object { $_.Name -match "^0+$" } |
measure-object).Count
## Install the font
new-itemproperty $key -Name ("0" * ($zeros + 1)) -Type string -Value "$FontName"
write-host "$FontName font installed successfully as a command window font."
@mysticentity
Copy link

Oh and yeah, windows does have it installed because I can see it under C:\Windows\Fonts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment