Freigeben über


Adding and Removing Fonts with Windows PowerShell

A while back the topic of scripting the installation and removal of fonts came up in an internal discussion.  It reminded me that someone had once shared with me Visual Basic 6 utilities for doing just that.  So I decided to see if I could transform these into working Windows PowerShell scripts in the same way that I had rewritten my utility to set the time zone.  Like my time zone utility, these utilities used Windows APIs to install and remove fonts.  Specifically they used AddFontResource and RemoveFontResource (plus PostMessage to notify running applications of the change).

Attached are the results of this effort.  Add-Font.ps1 can be used to install a single font file or a folder of fonts.  Remove-Font.ps1 can be used to uninstall a single font file installed in the \Windows\Fonts folder.  These script are written for Windows PowerShell version 2.  Run each script with the -help switch for usage details.  They have only been tested on my computer so like anything else posted to this blog, please test them first and use at your own risk.

 

Disclaimer: The information on this site is provided "AS IS" with no warranties, confers no rights, and is not supported by the authors or Microsoft Corporation. Use of included script samples are subject to the terms specified in the Terms of Use .

This post was contributed by Michael Murgolo, a Senior Consultant with Microsoft Services - U.S. East Region.

PS_Font_Scripts.zip

Comments

  • Anonymous
    January 01, 2003
    Robin:  I had errors in the help text.  I was originally going to have separate "-file" and "-folder" parameters.  I later decided to only use "-path" but I didn't fix the help text completely.  I fixed it in the updated versions now attached.  Use "-path" for both single font files or a folder of fonts.  The script will figure out which it is. Yves:  I looked at the script and found that where was I was trying to echo any errors that occurred for some operations actually caused an error.  This made the script seem like it didn't work the first time but it really did.  Of course, this would cause subsequent runs to fail with "not found" because it was removed.  I updated the script to fix this.  Try re-installing the font with the space in the file name and then removing it with the updated version.

    • Anonymous
      June 09, 2017
      I encountered the same error as José Freitas.It looks like it occurs in Add-SingleFont() where the variable $myFolder is assigned a null value but subsequently used without checking its value. $shell.Namespace($fileDir) does not give a non-null result for me when $fileDir is a UNC path.
    • Anonymous
      August 14, 2017
      Downloaded the scripts but last modified date still shows 2013 for both files? Is the currently uploaded archive really the latest?
  • Anonymous
    April 20, 2011
    Thanks for this script. Have you also a script or a extension for this script to install Type 1 Fonts?

  • Anonymous
    May 11, 2011
    Hi Michael, Thanks for developing these. I am trying to run Add-Font.ps1 on Windows 2008 R2 using the folder parameter, but am coming across the following error. Test-Path : Cannot bind argument to parameter 'Path' because it is an empty string. At \lonvsp01WisdomResourcesPowershellScriptsAdd-Font.ps1:549 char:19

  •     if ((Test-Path <<<<  $path -PathType Leaf) -eq $true)    + CategoryInfo          : InvalidData: (:) [Test-Path], ParameterBindingVa   lidationException    + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAl   lowed,Microsoft.PowerShell.Commands.TestPathCommand I find this unusual as I am using -folder, and yet it seems to be stuck at the point of evaluating whether the parameter is a file or not. I hope you can assist with this. Geoff
  • Anonymous
    June 26, 2013
    Just found this.  The answer is easy... just leave off the "-folder" portion... like this: Add-Font.ps1 "C:NewFonts"

  • Anonymous
    August 28, 2013
    Hi, ive tried the Scripts, but i got a Problem. I want to Remove an Font with spaces in the name. When im Calling the Script ist occures an Error. RemoveFont.ps1 -File "FonT name.ttf" or RemoveFont.ps1 -File FonT name.ttf or RemoveFont.ps1  "FonT name.ttf" or RemoveFont.ps1 FonT name.ttf Give me the Message: C:WindowsFontsFonT not found Best Regards Yves

  • Anonymous
    September 18, 2013
    Hi, I tried to run this in a startup GPO and the fonts are copied to the fonts directory and all reg-keys are created, but still the user can't see the fonts added. Any suggestions? This is on a Windows 7 machine. Best regards, Fredrik

  • Anonymous
    August 28, 2014
    Fredrik, if you're installing via a computer-based GPO, this might occur because the user's logon process registers available fonts at login time - any fonts subsequently installed in some other context (such as LOCALSYSTEM), would not be recognised until the next login.

    You could confirm if this is what's happening by logging the user off and on again, and see if the new fonts are now visible. If this is the case, then simply install the fonts with a user-based GPO instead, which will ensure that the newly registered fonts are available immediately without having to log in again.

    I made this discovery while figuring out a solution to push fonts via SCCM, hope it helps anyone else who ends up here.

    Cheers,

    -Ben.

  • Anonymous
    November 20, 2014
    So Ben, did you figure out how to do it with SCCM? I have the same problem - font not showing when installed via sccm.

  • Anonymous
    August 04, 2015
    Thank you Michel for your script, I'm using it on Windows 8.1 to allow users to install Fonts without turn off User Account Control (UAC). I use a cmd to call the PS1.

    start powershell -sta "C:WindowsInst_font.ps1" -path "C:NewFonts", the user only needs to add fonts to the folder and click on the shortcut.
    Thanks again for your work and for sharing the script.

  • Anonymous
    December 21, 2015
    Great! Thanks for the script.

  • Anonymous
    January 27, 2016
    So I have had these scripts for some time and just have not had much time to 'play'. We use Configuration Manager and I think these would be a godsend! Is there a way to have the script prompt the users for a folder that it could read from? You know something like when you open a word doc? Similarly with the removal of the fonts, a way to prompt for the font file names? Also how will this work with Windows 10? Using the script as an admin I had to comment out set-variable CSIDL_FONTS 0x14 -option constant as it wings errors.

    Again thanks for the GREAT scripts!!

  • Anonymous
    March 03, 2016
    Nice script but one prblem when i try to install fonts saved in a network share the script returns:
    An error occured installing 'Microsoft.PowerShell.CoreFileSystem::\srv-fsoftPortoSans-BoldItalic.otf'

    You cannot call a method on a null-valued expression.

  • Anonymous
    September 05, 2017
    Thank you for this!Deployed fonts as SCCM application with this script.Only error that I ran into while testing was with UNC paths, not a big problem (that might be why I had to deploy as application and not program).