问题
I have a folder filled with TTF files of custom fonts. I need to install them as system fonts using a powershell script (this is on Windows Server 2008 R2). Does anybody know how to do that in powershell? Thanks!
回答1:
It is quite simple. Take a look on the snippet below:
$FONTS = 0x14
$objShell = New-Object -ComObject Shell.Application
$objFolder = $objShell.Namespace($FONTS)
$objFolder.CopyHere("C:\test\Myfont.ttf")
And it should not require to restart/logoff...
The 0x14 value is the CLSID of the special folder.
In addition I just found this tutorial explaining each step above:
http://windowsitpro.com/scripting/trick-installing-fonts-vbscript-or-powershell-script
回答2:
Just wanted to post an alternative which doesn't require 0x14
to be hard coded into the script. Pass the file object to the function, and it will just run the "Install" based on where the file is:
Function Install-Font {
Param (
[Parameter(Mandatory=$true,ValueFromPipeline=$true)][System.IO.FileSystemInfo[]]$File
)
$shell = New-Object -ComObject Shell.Application
$File | % {
$Fonts = $shell.NameSpace($_.Directory.Name)
$font = $Fonts.ParseName($_.Name)
$font.InvokeVerb("Install")
}
}
回答3:
Using the Shell.Application
COM object doesn't work on Server Core (at least not on 2012 R2).
I had success by simply copying the font file to C:\Windows\Fonts
(in this case times.ttf) and then adding the corresponding registry entry with PowerShell:
New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts' -Name 'Times New Roman (TrueType)' -PropertyType String -Value times.ttf
Removal is the reverse of installation. The only downside is that a restart is required both after the font has been installed and also before it is uninstalled if an application has referenced it.
回答4:
Shell code has been known to fail on Remote and Build agents - if the comobjects using shell are failing and you are vetting via Remote or Build agents then you will need to use the framework classes to do this (reference)
## Add or Remove Font Files - only tested with TTF font files thus far
#<#
#=======================================================================================================
# ADD or REMOVE MULTIPLE FONT FILES [Using ComObjects]
#=======================================================================================================
# This code will install or uninstall a font using ComObject
# You Must Modify the following variables in order to work
# (A) $dirFiles ==> This is the source folder path that contains all of your font files
# (B) $InstallOrUninstall ==> $true = Install Font ... $false = UnInstall Font
#=======================================================================================================
# Define Working Variables
$dirFiles = "C:\Temp\Fonts"
$InstallOrUninstall = $false # $true = Install = 1 ...or... $false = UnInstall = 0
$srcFontFiles = Get-ChildItem "$($dirFiles)\Fonts"
$Fonts = (New-Object -ComObject Shell.Application).Namespace(0x14)
# Copy each file into the Font Folder or Delete it - Depends on the $InstallOrUninstall variable setting
ForEach($srcFontFile in $srcFontFiles)
{
$srcFontFileName = $srcFontFile.name
$srcFontFileFullPath = $srcFontFile.fullname
$targFonts = "C:\Windows\Fonts\$($srcFontFileName)"
If (Test-Path $targFonts -PathType any) { Remove-Item $targFonts -Recurse -Force } # UnInstall Font
If ((-not(Test-Path $targFonts -PathType container)) -and ($InstallOrUninstall -eq $true)) { $fonts.CopyHere($srcFontFileFullPath, 16) } # Install Font
}
#>
来源:https://stackoverflow.com/questions/16023238/installing-system-font-with-powershell