8
votes

anyone know how to install font files (.ttf, .TTF, .otf, .OTF, etc etc) through the command prompt on windows?

as i understand it, it requires moving the text file to the correct folder and then also creating a registry value i think? but I havent been able to find one that is confirmed working.

a note: I am using windows 8 so that might make a difference.

another note: what I am trying to do is batch install fonts that I ripped from MKV files. (so this will be a function that is part of a larger .bat file, i can post the code if needed)

8
There's no way to do it wuthout a third party tools (at least an additional DLL/EXE file). While you can manually add a font via file copy and modifying registry, the system won't still be aware of the new font and will need a system restart. - Jay

8 Answers

6
votes

maybe this is needed too:

reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts" /v "FontName (TrueType)" /t REG_SZ /d FontName.ttf /f
6
votes

You'll need to use a PowerShell or VB script. They basically re-use the shell components that do the same thing in Windows Explorer, and they don't need a reboot.

See here for a PowerShell script that installs all fonts from a directory for Windows 8.1 or earlier: https://social.technet.microsoft.com/Forums/fr-FR/winserverpowershell/thread/fcc98ba5-6ce4-466b-a927-bb2cc3851b59

Here is a similar script for Windows 10 (Windows Server 2019) that also updates the Windows Registry: https://social.technet.microsoft.com/Forums/en-US/0c94dcf5-b89d-42e5-a499-06313f46f88b/can-no-longer-install-fonts-via-script-in-windows-10-1809?forum=win10itprogeneral

Also, you'll need to run the script in admin mode. So if the PowerShell script is InstallFonts.ps1, your batch file needs to look like:

powershell -command "Set-ExecutionPolicy Unrestricted" 2>> err.out  
powershell .\InstallFonts.ps1 2>> err.out

Any powershell errors will appear in 'err.out' on the same folder as the script.

4
votes

When you install a font all it does is copy the .ttf file to %systemroot%\fonts and add an entry in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts. This can be automated with a batch file as follows

Rem fontinst.bat

copy akbar.ttf %systemroot%\fonts

regedit /s font.reg

The font.reg would contain the following:

REGEDIT4

\[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts\]

"Akbar Plain (TrueType)"="akbar.ttf"

Source: m.windowsitpro.com

1
votes

Have you tried copying them to the font's folder?

copy font.ttf %windir%\Fonts
0
votes

if you are a python fan, following script does the job. This script generates a vbscript for font installation. Searches all the sub-folders for ttf fonts and installs it. You don't need to move any font files.

import os
import subprocess
import time

# vb script template
_TEMPL  = """ 
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace("%s")
Set objFolderItem = objFolder.ParseName("%s")
objFolderItem.InvokeVerb("Install")
"""


vbspath = os.path.join(os.getcwd(), 'fontinst.vbs')

for directory, dirnames, filenames in os.walk(os.getcwd()):
    for filename in filenames:
        fpath = os.path.join(directory, filename)

        if fpath[-4:] == ".ttf": # modify this line for including multiple extension
            with open(vbspath, 'w') as _f:
                _f.write(_TEMPL%(directory, filename))
            subprocess.call(['cscript.exe', vbspath])
            time.sleep(3) # can omit this

 os.remove(vbspath)  # clean

Run this python script on the root folder

0
votes

Batch file sample. It works in the current directory.

IF  "%*" NEQ "" SET FONT=%*  (

FOR /F %%i in ('dir /b "%FONT%*.*tf"') DO CALL :DEST %%i

) else (

EXIT

)

:DEST

SET FONTFILE=%~n1%~x1
SET FONTNAME=%~n1


IF "%~x1"==".ttf" SET FONTTYPE=TrueType
IF "%~x1"==".otf" SET FONTTYPE=OpenType

ECHO FILE = %FONTFILE%
ECHO NAME = %FONTNAME:-= %
ECHO TYPE = %FONTTYPE%

fontview  %~dp0%FONTFILE%  


GOTO :EXIT
0
votes

I solved the task in this way:

suppose you have to install many fonts in subfolders with the following structure recursively:

\root_folder
    Install_fonts.cmd
    \font_folder_1
        font_1.ttf
        font_2.otf
    \font_folder_2
        font_3.ttf
        font_4.otf
    \font_folder_3
        font_5.ttf
        font_6.otf

To do that, I downloaded the FontReg.exe tool on my Desktop (change the path in the Install_fonts.cmd file if it is located somewhere else) and I used it in a Install_fonts.cmd batch script like the following, located in root_folder (change also its name in the Install_fonts.cmd file, if different):

@echo off
set back=%cd%
for /d %%i in (%USERPROFILE%\Desktop\root_folder\*) do (
cd "%%i"
echo current directory:
cd
start /wait %USERPROFILE%\Desktop\fontreg-2.1.3-redist\bin.x86-64\FontReg.exe /move
timeout /t 1 /nobreak >nul
)
cd %back%
echo Process completed!
pause

So, you have to run Install_fonts.cmd into root_folder as administrator, to automate the fonts installation process.

Cheers

0
votes

So a colleague and I found a powershell solution that requires no admin rights, and does not show any prompts. You can use the name of the font-file to install and uninstall. This makes it especially useful for scripting.

Install:

# Install-Font.ps1
param($file)

$signature = @'
[DllImport("gdi32.dll")]
 public static extern int AddFontResource(string lpszFilename);
'@

$type = Add-Type -MemberDefinition $signature `
    -Name FontUtils -Namespace AddFontResource `
    -Using System.Text -PassThru
   
$type::AddFontResource($file)

Uninstall:

# Uninstall-Font.ps1
param($file)

$signature = @'
[DllImport("gdi32.dll")]
public static extern bool RemoveFontResource(string lpszFilename);
'@

$type = Add-Type -MemberDefinition $signature `
    -Name FontUtils -Namespace RemoveFontResource `
    -Using System.Text -PassThru
   
$type::RemoveFontResource($file)

You can use them like this from cmd or powershell:

> powershell -executionpolicy bypass -File .\Install-Font.ps1 .\myfonts\playfair-display-v22-latin-regular.ttf
> powershell -executionpolicy bypass -File .\Uninstall-Font.ps1 .\myfonts\playfair-display-v22-latin-regular.ttf

The solution is based on https://www.leeholmes.com/powershell-pinvoke-walkthrough/ and uses native Win32 functions (gdi32.dll). https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-addfontresourcew