Windows: install fonts from cmd/.bat file

46,729

Solution 1

maybe this is needed too:

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

Solution 2

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.

Solution 3

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

Solution 4

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

Solution 5

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

copy font.ttf %windir%\Fonts
Share:
46,729
Author by

begna112

Updated on July 23, 2022

Comments

  • begna112 5 months

    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)

  • Twonky
    Twonky almost 3 years
    Which part of the PowerShell script is supposed to add the registry key?
  • Yoshi almost 3 years
    @Twonky I've updated my answer to include a link to a PS script that also sets the registry key for the font. This is needed for Windows 10 or Windows Server 2019.
  • Twonky
    Twonky almost 3 years
    Copying to "%SYSTEMROOT%\Fonts" plus this worked with Windows 10. No PowerShell needed. Just run these commands with admin permissions.
  • Andry
    Andry almost 2 years
    That code might be not enough. The Windows Shell verb string is dependent on localized version of the Windows.
  • Bowo about 1 year
    Thanks for this! This is very useful especially when you're want to install Font from CI/CD script. Good work!
  • Aalex Gabi
    Aalex Gabi 10 months
    You should know that AddFontResource is not installing the font but registering it for usage during the session which means font is not present anymore after reboot. In some cases this is desired but not in all cases. Installed fonts need to be in a special system folder (Windows/Fonts or in user fonts folder) and have a registry key in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts (or the user equivalent)