54

Is it possible to install fonts from the command prompt on Windows? If yes, what is the command?

I tried copy [fontname].ttf C:\Windows\Fonts\ and it said copying was complete, but I could neither find the said fonts in the Fonts folder nor find them in the font list of any program so that certainly didn't work. (Although I was able to delete the said fonts from the Fonts folder afterwards)

Andrea
  • 1,536
Mussnoon
  • 1,118

15 Answers15

38

It's possible but you have to write a Windows shell script to do that. Copying alone won't install the font: you also need to register the font, e.g.

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

Alternatively you can the following lines of code to suit your needs; save it as a .vbs file and then execute it.

Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace("<Folder or Share Location>")
Set objFolderItem = objFolder.ParseName("<TTF File Name>")
objFolderItem.InvokeVerb("Install")

Example:

Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace("C:\Windows\Font")
Set objFolderItem = objFolder.ParseName("Myriad Pro.ttf")
objFolderItem.InvokeVerb("Install")

Yet another alternative is to install fonts "temporary", just for current user session. The idea is to run fontview.exe for each font, which makes it available for other Windows applications:

for /F "delims=;" %%a in ('dir C:\ExtraFonts /B /A-D-H-S /S') do fontview %%a

See the complete solution here.

phuclv
  • 30,396
  • 15
  • 136
  • 260
GeneQ
  • 5,087
30

In Powershell this can be as simple as:

$fonts = (New-Object -ComObject Shell.Application).Namespace(0x14)
dir fonts/*.ttf | %{ $fonts.CopyHere($_.fullname) }
Guss
  • 1,171
7

Similar to GeneQ's solution, here is a version doing it for all .ttf files in the script's directory:

Set ofso = CreateObject("Scripting.FileSystemObject")
SourceFolder = ofso.GetParentFolderName(Wscript.ScriptFullName)

Const FONTS = &H14&

Set objShell  = CreateObject("Shell.Application")
Set oSource   = objShell.Namespace(SourceFolder)
Set oWinFonts = objShell.Namespace(FONTS)

' Lame VBscript needs 4 f*ing lines instead of "if (/\.ttf$/i) " ...
Set rxTTF = New RegExp
rxTTF.IgnoreCase = True
rxTTF.Pattern = "\.ttf$"

FOR EACH FontFile IN oSource.Items()
    IF rxTTF.Test(FontFile.Path) THEN   
        oWinFonts.CopyHere FontFile.Path
    END IF
NEXT
phuclv
  • 30,396
  • 15
  • 136
  • 260
mivk
  • 4,015
5

You can also use the FontReg utility to install fonts from a command prompt.

afrazier
  • 23,505
3

I created a simple powershell script for this purpose. similar to those mentioned above just run this script in a directory containing ttf or otf fonts, it will find them in any subdirecories as well automatically prompts the UAC dialog for elevated priveleges while installing fonts.

# install-fonts.ps1
$fonts = (New-Object -ComObject Shell.Application).Namespace(0x14)

$here = $MyInvocation.PSScriptRoot

$font_files = Get-ChildItem -Path $here -Recurse | Where-Object { $.Extension -ilike "*.otf" -or $.Extension -ilike "*.ttf" }

foreach($f in $font_files) { $fname = $f.Name Write-Host -ForegroundColor Green "installing $fname..." $fonts.CopyHere($f.FullName) }

2

Create a script file called InstallFonts.vbs in my case I put it in C:\PortableApps\InstallFonts\ IN the below code replace "SomeUser" with the username of the person you want to be able to install fonts. Then make the Appropriate "install Fonts" folder on their desktop.

Set ofso = CreateObject("Scripting.FileSystemObject")
'SourceFolder = ofso.GetParentFolderName(Wscript.ScriptFullName)
SourceFolder = "C:\Users\SomeUser\Desktop\Install Fonts"


Const FONTS = &H14&

Set objShell  = CreateObject("Shell.Application")
Set oSource   = objShell.Namespace(SourceFolder)
Set oWinFonts = objShell.Namespace(FONTS)

' Lame VBscript needs 4 f*ing lines instead of "if (/\.ttf$/i) " ...
Set rxTTF = New RegExp
rxTTF.IgnoreCase = True
rxTTF.Pattern = "\.ttf$"

FOR EACH FontFile IN oSource.Items()
    IF rxTTF.Test(FontFile.Path) THEN   
        oWinFonts.CopyHere FontFile.Path
    END IF
NEXT

Now create a shortcut on their desktop that is as follows...

C:\Windows\System32\runas.exe /user:Administrator /savecred "wscript C:\PortableApps\InstallFonts\InstallFonts.vbs"

Note that I used "Administrator". I enabled it and assigned it a password. I suppose you could use any administrator account for this. First time you run the shortcut you will be prompted for the administrator password. Every time after it will just work.

If it does not prompt you for a password run the shortcut from a cmd prompt it should prompt you then.

I cannot promise you how secure this is as in if they could use it to run elevated code. However it is a solution.

phuclv
  • 30,396
  • 15
  • 136
  • 260
1

Guss & EvgeniySharapov really is the easiest approach.

  1. Download fonts
  2. Unzip files
  3. Open Powershell and navigate to the target directory
  4. Execute code in target directory to recursively install fonts
$fonts = (New-Object -ComObject Shell.Application).Namespace(0x14)
Get-ChildItem -Recurse -include *.ttf | % { $fonts.CopyHere($_.fullname) }
1

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.

I posted it over at stackoverflow: https://stackoverflow.com/a/67903796/1635906

htho
  • 111
0

As said earlier by GeneQ, this is how you proceed (I've tested it)

  1. Open a command line with administrator privileges
  2. Use the command:

    for /F "delims=;" %a in ('dir C:\FontsDir /B /A-D-H-S /S') do fontview
    %a
    

Where C:\FontsDir is the directory where your tff files are stored. Once executed "fontview" windows will be opened as much as the number of tff files inside "FontsDir" directory. You have just to click on "Install" button and there you are! your fonts are installed on you system

Hope it would help someone

phuclv
  • 30,396
  • 15
  • 136
  • 260
Sam Doxy
  • 101
0

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

To supplement the first answer for Windows 10, you need to copy the fonts to C:\Windows\Fonts and then also add to the following windows registry location for EACH font you have.

HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts
HKLM\SOFTWARE\WOW6432Node\Microsoft\Windows NT\CurrentVersion\Fonts

Ex. From first answer:

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

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

Only adding to HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts did not work for me.

Make sure to find the actual font name. You can find that out by manually clicking on each font or adding it manually to registry first to find out the actual name that gets added and then update your batch file.

0

FontReg fixed the issue for me: http://code.kliu.org/misc/fontreg/

Command Line: FontReg.exe /copy

0

if you are using msys2, this script worked for me:

#!/bin/bash

Check for dry run option

DRY_RUN=false FONT_URL=""

if [ "$1" = "-n" ]; then DRY_RUN=true shift FONT_URL="$1" else FONT_URL="$1" fi

Check if the URL argument is provided

if [ -z "$FONT_URL" ]; then echo "Usage: $0 [-n] <font_url>" exit 1 fi

Variables

FONT_NAME=$(basename "$FONT_URL" | sed 's/%20/ /g') # Decode URL-encoded spaces FONT_DIR_WIN=$(cygpath -w "$USERPROFILE/AppData/Local/Microsoft/Windows/Fonts") FONT_DIR_CYG=$(cygpath -u "$FONT_DIR_WIN") REG_PATH="/HKEY_CURRENT_USER/Software/Microsoft/Windows NT/CurrentVersion/Fonts"

Functions

download_font() { local url=$1 local path=$2 wget -O "$path" "$url" if [ $? -ne 0 ]; then echo "Failed to download '$url' to '$path'." exit 1 fi }

determine_font_type() { local file_path=$1 local mime_type=$(file -b --mime-type "$file_path") case $mime_type in font/sfnt) echo "TrueType" ;; application/x-font-otf) echo "OpenType" ;; *) echo "Unknown" ;; esac }

ensure_font_dir_exists() { local dir=$1 if $DRY_RUN; then echo "Would create directory '$dir' if it doesn't exist." else mkdir -p "$dir" if [ $? -ne 0 ]; then echo "Failed to create directory '$dir'." exit 1 fi fi }

perform_action() { if $DRY_RUN; then echo "Would $1" else eval "$2" if [ $? -ne 0 ]; then echo "Failed to $1." exit 1 fi fi }

print_actions() { local font_dir=$1 local font_name=$2 local font_type=$3 echo "Would perform the following actions:" echo "1. Create directory '$font_dir' if it doesn't exist." echo "2. Download '$font_name' to '$font_dir/$font_name'." echo "3. Add registry key '$REG_PATH/$font_name ($font_type)'." echo "4. Set registry key '$REG_PATH/$font_name ($font_type)' to '$font_dir/$font_name'." }

perform_install_actions() { local font_dir=$1 local font_name=$2 local font_type=$3 ensure_font_dir_exists "$font_dir" perform_action "download '$FONT_URL' to '$font_dir/$font_name'" "download_font '$FONT_URL' '$font_dir/$font_name'" perform_action "add registry key '$REG_PATH/$font_name ($font_type)'" "regtool add '$REG_PATH/${font_name// /\ } ($font_type)'" perform_action "set registry key '$REG_PATH/$font_name ($font_type)' to '$font_dir/$font_name'" "regtool set '$REG_PATH/${font_name// /\ } ($font_type)' '$(cygpath -w "$font_dir/$font_name")'" }

Main script logic

if $DRY_RUN; then # Create temporary directory TEMP_DIR=$(mktemp -d) trap "rm -rf '$TEMP_DIR'" EXIT echo "Temporary directory: $TEMP_DIR"

# Download font to temp directory
download_font &quot;$FONT_URL&quot; &quot;$TEMP_DIR/$FONT_NAME&quot;

# Determine font type
FONT_TYPE=$(determine_font_type &quot;$TEMP_DIR/$FONT_NAME&quot;)

# Print MIME type in dry run mode
echo &quot;MIME type: $(file -b --mime-type &quot;$TEMP_DIR/$FONT_NAME&quot;)&quot;

# Print actions
print_actions &quot;$FONT_DIR_WIN&quot; &quot;$FONT_NAME&quot; &quot;$FONT_TYPE&quot;

# Delete the downloaded font file in dry run mode
rm -f &quot;$TEMP_DIR/$FONT_NAME&quot;
echo &quot;Deleted downloaded font file: $TEMP_DIR/$FONT_NAME&quot;

else # Ensure font directory exists ensure_font_dir_exists "$FONT_DIR_CYG"

# Download font to font directory
download_font &quot;$FONT_URL&quot; &quot;$FONT_DIR_CYG/$FONT_NAME&quot;

# Determine font type
FONT_TYPE=$(determine_font_type &quot;$FONT_DIR_CYG/$FONT_NAME&quot;)

# Perform install actions
perform_install_actions &quot;$FONT_DIR_CYG&quot; &quot;$FONT_NAME&quot; &quot;$FONT_TYPE&quot;

echo &quot;Font installed successfully!&quot;

fi

This installs fonts on current user, does not need admin rights. You can run it as

./install_font.sh -n  https://github.com/powerline/fonts/raw/refs/heads/master/3270/3270-Regular.ttf

for dry run or delete the -n for not dry run, for now it does it from an url, not for local fonts, because thats what I needed at the time

Rainb
  • 255
-1

I spent a lot of time to find a way for installing font without restart. Finally I found this: ClickFont. It's an easy and exact solution.

ClickFont allows easy installation of TrueType, OpenType and PostScript fonts with just two mouse clicks, from anywhere in the system. All it takes is a right-click on a font or folder.

-1

You didn't list your Windows version, but I assume you're running Vista or 7. Copying to that directory requires administrative privileges. Try what you did again, but use an Elevated Command Prompt instad this time.

jsejcksn
  • 4,581