Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  Advanced Search 


February 27, 2008

The Trick to Installing Fonts with a VBScript or PowerShell Script

Using the Shell.Application object is key
RSS
View this exclusive article with VIP access -- click here to join | See More Installation Articles Here | Reprints
Or sign up for our VIP Monthly Pass!

Download the Code Here

 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.

End of Article



Reader Comments
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

AbqBill March 04, 2008 (Article Rating: )


You must log on before posting a comment.

If you don't have a username & password, please register now.




Top Viewed ArticlesView all articles
The Memory-Optimization Hoax

Don't believe the hype. At best, RAM optimizers have no effect. At worst, they seriously degrade performance. ...

Managing Virtual Sprawl

As some wise person once said, nothing is ever truly free. Such is the case with VMs, which can quickly mutate from a cost-reducing Dr. Jekyll into a time-consuming, profligate nightmare that would do Mr. Hyde proud. ...

What service packs and fixes are available?

...


Related Articles PowerShell One-Liners for Managing the File System

Migrating to PowerShell? Don't Scrap Your Existing Code Just Yet

Scripting Solutions with WSH and COM: Short, Sharp Scripts

Understanding VBScript: The Windows Shell Object Model

Related Events Check out our list of Free Email Newsletters!

Scripting eBooks Keeping Your Business Safe from Attack: Encryption and Certificate Services

Best Practices for Managing Linux and UNIX Servers

Building an Effective Reporting System

Related Scripting Resources Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.


ADS BY GOOGLE SPONSORED LINKS FEATURED LINKS

Critical Challenges of ESI & Email Retention
Are you storing too much electronic information? Get expert legal advice and better understanding of what you are required to do as an IT professional.

Become a fan of Windows IT Pro on Facebook!
Join us on Facebook and be a fan of Windows IT Pro!

Sustainable Compliance: Are You Having a Resource Crisis?
Read this white paper to examine trends in compliance and security management and review approaches to reducing the cost and operational burden of compliance.

Rev Up Your IT Know-How with Our Recharged Magazine!
The improved Windows IT Pro provides trusted IT content with an enhanced new look and functionality! Get comprehensive coverage of industry topics, expert advice, and real-world solutions—PLUS access to over 10,000 articles online. Order today!

Get It All with Windows IT Pro VIP
Stock your IT toolbox with every solution ever printed in Windows IT Pro and SQL Server Magazine plus bonus Web-exclusive content on hot topics. Subscribe to receive the VIP CD and a subscription to your choice of Windows IT Pro or SQL Server Magazine!



Order Your Fundamentals CD Today!
Gain an introduction to Exchange, learn server security requirements, and understand how unified communications can play a role in your messaging strategies with this free Exchange CD.
Windows IT Pro Home Register About Us Affiliates / Licensing Media Kit Contact Us/Customer Service  
SQL Connected Home IT Library SuperSite FAQ Wininfo News
Europe Edition Office & SharePoint Pro Windows Dev Pro Windows Excavator 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2008 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing