-
-
Save nathanverrilli/d3f12fbad87953e28aae78b5f191cdab to your computer and use it in GitHub Desktop.
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." | |
noted: I did install that font, When I tried to re-install, I got this error:
[Window Title]
Install Font
[Main Instruction]
The 'Unifont Smooth' font is already installed. Do you want to replace it?
[Yes] [No]
The filename parameter looks wrong. When I downloaded & tested with Unifont Medium, the command line was
./InstallFont -FontName Unifont -FontFile unifont-12.1.02.ttf
and that worked correctly for me.
Also, this needs to be an elevated shell (as it is installing stuff to the registry).
Please let me know if that helps!
UPDATE 7/30/2019 Added some additional error checking, expanded usage with an example, made some assumptions explicit
Another fiddly fix, but tested this time. Should be good now.
Why not install the font anyway? Since you are asking for it.
See the following snippet I used on my system
## Find out if $FontName is installed on the system
$fontFileName = join-path $env:windir "Fonts\$FontFile"
if(!(test-path $fontFileName))
{
write-host "Installing $FontFile to $fontFileName"
Copy-Item -path $FontFile -destination $fontFileName
}
Thanks for creating this BTW!
You're welcome!
I didn't install the font file because installing a font to windows seemed like a solved problem. Plus, a lot of the error checking and help text got added in response to other's comments. The original script had a number of implicit, untested assumptions and insufficient guidance for anyone not already familiar with the process.
Cheers,
N
So I installed a font called "Terminal" which has it's file named vgaoem.fon - so I used the command like this, "./ConsolFont.ps1 'Terminal' -FontFile vgaoem.fon" I then got this message, "Terminal font installed successfully as a command window font." so I assumed that it worked however not only do I not see it anywhere, I even tried to manually select it via the properties in WindowsPowershell and Command Prompt through the fonts tab and don't see it. Am I doing something wrong?
Here is the full log,
"000 : Terminal
PSPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows
NT\CurrentVersion\Console\TrueTypeFont
PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows
NT\CurrentVersion\Console
PSChildName : TrueTypeFont
PSDrive : HKLM
PSProvider : Microsoft.PowerShell.Core\Registry
Terminal font installed successfully as a command window font."
Hi Taylor!
A .FON
file is not a true type font, but (if I recall correctly) an executable file with (potentially) multiple old-style .FNT
fonts. Supposedly this does support some vectorized glyphs, but I've only ever seen bitmapped fonts (I think. It's been a long time since I played with these).
This installation script only works for modern truetype fonts.
I suspect it did get installed as a registry key (HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Console\TrueTypeFont
) but PowerShell can't use it because it's listed as a TrueTypeFont
rather than under RasterFonts
DANGER use Regedit
You will need to use Regedit to fix/install the font. Please remember that you must run Regedit in administrator mode to make changes (rather than view) the registry. I am certain you are aware of the infinite amount of damage and destruction misuse of regedit can cause to a windows installation, but I feel strongly about putting DANGER markers anytime something is that dangerous. One never knows who is going to read this or in what context.
Rollback:
- Remove the invalid key (with the .FON file) at
HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Console\TrueTypeFont
Possible Fix
Please note that there might be some key naming convention (like the string of 0
characters for truetype fonts) that I am unaware of.
as in, it MIGHT work but I do not know enough to say for certain:
- Try installing the key at
HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Console\RasterFonts
. Be certain that Windows has already installed the font.
Suggested Course of Action
If I were trying to do this, I would experiment in an VM first, but then I am a cautious soul who would rather spend 3 minutes spinning up a VM to test with than 3 days rebuilding my production machine.
Please let me know if this is helpful!
Cheers,
N
Hi Taylor!
A
.FON
file is not a true type font, but (if I recall correctly) an executable file with (potentially) multiple old-style.FNT
fonts. Supposedly this does support some vectorized glyphs, but I've only ever seen bitmapped fonts (I think. It's been a long time since I played with these).This installation script only works for modern truetype fonts.
I suspect it did get installed as a registry key (
HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Console\TrueTypeFont
) but PowerShell can't use it because it's listed as aTrueTypeFont
rather than underRasterFonts
DANGER use Regedit
You will need to use Regedit to fix/install the font. Please remember that you must run Regedit in administrator mode to make changes (rather than view) the registry. I am certain you are aware of the infinite amount of damage and destruction misuse of regedit can cause to a windows installation, but I feel strongly about putting DANGER markers anytime something is that dangerous. One never knows who is going to read this or in what context.
Rollback:
* Remove the invalid key (with the .FON file) at `HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Console\TrueTypeFont`
Possible Fix
Please note that there might be some key naming convention (like the string of
0
characters for truetype fonts) that I am unaware of.as in, it MIGHT work but I do not know enough to say for certain:
* Try installing the key at `HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Console\RasterFonts`. Be certain that Windows has already installed the font.
Suggested Course of Action
If I were trying to do this, I would experiment in an VM first, but then I am a cautious soul who would rather spend 3 minutes spinning up a VM to test with than 3 days rebuilding my production machine.
Please let me know if this is helpful!
Cheers, N
Followed what you said an unfortunately I still don't see it under fonts when I right click on powershell after running as administrator and going to fonts under properties. I also changed the way it's named to follow the other stuff in the registry and it still didn't work. It's fine though, if it's not going to work i'm not super worried about it. Just thought I'd give it a try.
Oh and yeah, windows does have it installed because I can see it under C:\Windows\Fonts
Tried to install "Unifont Smooth" to
cmd
, but got this error:Could you give me a clue?