Skip to main content

Installing Fonts with PowerShell

·281 words·2 mins
system-administration tech application-packaging fonts powershell sccm sysadmin technology windows windows-10 windows-7
James Pettigrove
Author
James Pettigrove
Cloud Engineer with a focus on Microsoft Azure

Not normally the application packaging type but some time ago it fell to me to package up some open source fonts and deploy them via System Centre Configuration Manager across the enterprise. While this seems like a simple task at first, fonts are treated in a special way in Windows and we were also under strict legal instructions to comply with the open source licensing requirements of the font which were

  • User must have the license terms & conditions displayed to them prior to install
  • If the user accepts the terms & conditions, install plus deploy a copy of said terms & conditions to the endpoint

With this in mind and after a couple of trial and error bumps a long the road I came up with the below Install-Fonts.ps1 Powershell script.

<#
.DESCRIPTION
Powershell script that prompts user to accept license followed by installation of font and license files
.PARAMETERS
None - execute directly from Powershell
.Version
1.3
.Author
James Pettigrove
.Compatibility
Windows 7, Windows 10
.Release Date
31/01/2017
.Notes
v1.0, 20160131 - Initial Version
v1.1, 20160201 - Added job to dismiss overwrite file prompts when installing the fonts
v1.2, 20160201 - Swapped out previous install and skip overwriting method
v1.3, 20160301 - Changed install method again to one that is compatible down to PS2.0
#>
## Load necessary assembly
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
## Vendor font license that will be displayed to the user for acceptence
$license = "LICENSE TEXT"
## Output License to user for acceptence
$output = [System.Windows.Forms.MessageBox]::Show("$license
Do you agree to the above Terms & Conditions?"
, "LICENSE NAME AND VERSION" , 4)
## If user accepts, continue
if ($output -eq "YES" )
{
## 0x14 is a special system folder pointer to the path where fonts live, and is needed below.
$fonts = 0x14
## Path to font files
$fontsPath = ".\"
## Make a refrence to Shell.Application
$objShell = New-Object -ComObject Shell.Application
$objFolder = $objShell.Namespace($fonts)
## Used for debugging
$copyOptions = 4 + 16
## Copy file in $($file.fullname) PS2.0 friendly format
$copyFlag = [String]::Format("{0:x}", $copyOptions)
## Loop through each directory in the specified path looking for font files
foreach($file in $(Get-ChildItem -Path $fontsPath -Include *.ttf,*.otf,*.fon,*.fnt -Recurse))
{
## Confirms font doesn't exist already before copying, if exists, don't copy
If (test-path "c:\windows\fonts\$($file.name)")
{}
Else
{
$copyFlag = [String]::Format("{0:x}", $copyOptions)
$objFolder.CopyHere($file.fullname, $copyOptions)
}
}
## Copy license file to workstation to meet licensing terms
$licensePath = ".\LICENSEFILE.txt"
$licenseDestination = "$env:programfiles\VENDOR\FONT\LICENSEFILE.txt"
New-Item -ItemType File -Path $licenseDestination -Force
Copy-Item $licensePath $licenseDestination -Force
}
## If user does not accept, exit script
else
{
Exit 1
}

To use, initiate the script from the same directory as your font(s) file together with a text document labelled LICENSEFILE.txt which contains the terms & conditions. The contents of the text file should be pasted into the following line:

## Vendor font license that will be displayed to the user for acceptence
$license = "LICENSE TEXT"

by replacing LICENSE TEXT so it is displayed to the user to allow them to accept prior to the installation kicking off. The license name and version should also be added to the Powershell script in the following line:

## Output License to user for acceptence 
$output = [System.Windows.Forms.MessageBox]::Show("$license
Do you agree to the above Terms & Conditions?"
 , "LICENSE NAME AND VERSION" , 4)

by replacing LICENSE NAME AND VERSION.

The above two elements could be made to be a bit more dynamic but hey, that’s what version 2.0 is for :)

Related

Legacy BIOS and UEFI PXE coexistence
·701 words·4 mins
system-administration tech bios dhcp powershell pxe sccm sysadmin technology uefi windows-server-2012-r2
More and more enterprises are moving towards the modern UEFI devices in their fleet, whether that be desktops, laptops or convertibles.
Export users of one AD group and import to another
·196 words·1 min
system-administration tech active-directory powershell sysadmin technology windows
It’s quite common for members of one Active Directory security group to be replicate in another.
The Group Policy Client service failed to logon - Access is denied
·157 words·1 min
system-administration tech group-policy sysadmin technology windows windows-7
Access is denied is a truly wonderful error.