Subscribe to Windows IT Pro

 

Get Newsletters

  • Get the Latest News
  • Product Updates
  • Helpful Tricks
  • Productivity Tips

Subscribe Now!

February 27, 2008 12:00 AM

The Trick to Installing Fonts with a VBScript or PowerShell Script

Using the Shell.Application object is key
Windows IT Pro
InstantDoc ID #97911
Rating: (1)
Downloads
97911.zip

 Executive Summary:

Windows automatically registers fonts you drag and drop into the Fonts folder. If you use a VBScript or Windows PowerShell script rather than Windows Explorer to copy fonts into that folder, you don't get the automatic registration unless you use the Shell.Application object. Here are the three steps you need to follow when using the Shell.Application object to install fonts through a VBScript or Windows PowerShell script.

Windows automatically recognizes fonts you drag and drop into the Fonts folder. However, if you try to install fonts by copying the file at a command prompt or through a script, you can't see them in the Fonts folder or in the Windows font listings. The problem is that fonts need to be registered. When you drag and drop them into the Fonts folder, the Windows Explorer shell copy operation triggers the registration process. If you don't use Windows Explorer, you don't get the automatic registration.

Fortunately, you can use Windows Explorer to register fonts when copying them through a script. The trick is to use the Shell.Application COM object, which is essentially what Windows Explorer uses during a drag-and-drop operation. With the Shell.Application object, you can connect to special namespaces in Windows such as the Fonts folder and perform special operations (e.g., copying) in that folder. You won't see a file copy onscreen, but it works.

Here are the steps you need to follow:

  1. Get a reference to the Shell.Application object.
  2. Use the Shell.Application object's NameSpace method to connect to the Fonts folder. Each special namespace has a numeric identifier known as a CSIDL that you use with the NameSpace method. For the Fonts folder, the CSIDL is 20. Although I won't be covering other folders or the techniques for accessing them here, if you're interested in CSIDL details, you can see some of the values listed in the ShellSpecialFolderConstants enumeration in the ShellSpecialFolderConstants Enumerated Type Web page (http://msdn2.microsoft.com/en-us/library/bb774096.aspx). For programming use, these numeric constants are deprecated in favor of globally unique identifiers (GUIDs) called KNOWNFOLDERIDs, which identify standard folders registered with the system. (For more information about these GUIDs, go to http://msdn2.microsoft.com/en-us/library/bb762584.aspx.) I'm using the CLSID values here because they're easier to use and backward compatible.
  3. Use the Shell.Application object's CopyHere method to copy the file.

For example, the following VBScript code snippet installs the font in the file ManchuFont2005.ttf from the SourceForge Manchu Font project (http://sourceforge.net/project/showfiles.php?group_id=118623):

Set sa = CreateObject("Shell.Application")
Set fonts  = sa.NameSpace(20)
fonts.CopyHere "C:\tmp\ManchuFont2005.ttf"

You can install the same font with this PowerShell command:

$sa =  new-object -comobject shell.application
$Fonts =  $sa.NameSpace(0x14)
$Fonts.CopyHere ("C:\tmp\ManchuFont2005.ttf")

In this command, I'm using the hexadecimal equivalent of 20—that is, 0x14—as the CLSID value. With PowerShell, you can extend this operation and install as many fonts as you want by using the pipeline and the ForEach-Object cmdlet, like this:

gci  "C:\dsp\Special Fonts\*.ttf" | %{$fonts.CopyHere($_.FullName)}

You can even put the PowerShell code into a script, as Install-Font.ps1 in Listing 1 shows. This means the only thing you need to do is get a collection of fonts and pipe them into Install-Font.ps1 using code such as

gci  "C:\dsp\Special Fonts\*.ttf" | Install-Font

Note that installing fonts is a privileged operation in Windows Vista. If you run the script from a nonprivileged account, the process won't necessarily fail; the CopyHere attempt triggers the User Account Control (UAC) dialog box, allowing it to work. Unfortunately, this happens for each and every font file in the installation set. I also recently encountered a situation on 64-bit Vista machine in which the UAC dialog box wasn't invoked and the installation silently failed. For these reasons, it's better to open a PowerShell session as administrator and use the script from there to install the fonts without any prompting.

 Share Your Scripting Experiences:

Share your scripting discoveries, comments, solutions to problems, and experiences with products. Email your contributions to r2r@scriptingprovip.com. Please include your full name and phone number. We edit submissions for style, grammar, and length. If we print your submission, you'll get $100.

Related Content:

ARTICLE TOOLS

Comments
  • Bill
    4 years ago
    Mar 04, 2008

    Hi Alex,

    Thanks for writing that -- I wish I'd seen it last week. I had three machines that I needed to install about 250 fonts. I copied the fonts to the machines, but the fonts didn't show in their list boxes (all of the users run as standard users, not admins). When I logged on as an admin and opened up systemroot\\Fonts in an Explorer window, the fonts then populated. (I'm glad it was only three machines.)

    I'm going to use your technique the next time I need to install fonts on several machines -- then I can do it without needing to log onto each machine.

    Thanks!

    Bill

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

White Papers

Get your Windows 7 deployment off to the right start by implementing PC lockdown. A locked-down environment is easier and cheaper to support since users are less likely to make unnecessary changes to the core system configuration - read more here!

Essential Guides

Is your iSCSI "lossy"? The reality is that most off-the-shelf Ethernet hardware deployed for iSCSI can lose packets, resulting in slow performance or application downtime. Learn how to assess your current iSCSI infrastructure and engineer an advanced iSCSI SAN infrastructure.

Web Seminars

What's the best way to keep your network safe from malware? In this web seminar, security expert Greg Shields suggests an alternative method to the traditional blacklisting approach that is common with anti-virus and anti-malware solutions.

eLearning Series

We bring the experts direct to you to share their real-world perspective and expertise. During each event, three sessions stream in real time, so you can learn, ask questions, and get solutions.
Upcoming event: Getting the Most with Exchange 2010 with Paul Robichaux

Subscribe to Windows IT Pro!

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.