使用Powershell安装系统字体

时间:2013-04-15 19:25:32

标签: powershell windows-server-2008 true-type-fonts

我有一个文件夹,里面装有自定义字体的TTF文件。我需要使用powershell脚本将其安装为系统字体(这在Windows Server 2008 R2上)。有人知道如何在powershell中做到这一点吗? 谢谢!

4 个答案:

答案 0 :(得分:16)

这很简单。看看下面的片段:

$FONTS = 0x14
$objShell = New-Object -ComObject Shell.Application
$objFolder = $objShell.Namespace($FONTS)
$objFolder.CopyHere("C:\test\Myfont.ttf")

它不应该要求重启/注销......

0x14值是特殊文件夹的CLSID。

此外,我刚刚发现本教程解释了上述每一步:

http://windowsitpro.com/scripting/trick-installing-fonts-vbscript-or-powershell-script

答案 1 :(得分:2)

只是想发布一个不需要grid.arrange(grobs = myplotslist, ncol=2, layout_matrix=cbind(c(1,2), c(3,4))) 硬编码到脚本中的替代方案。将文件对象传递给函数,它将运行" Install"根据文件的位置:

0x14

答案 2 :(得分:0)

使用Shell.Application COM对象在Server Core上不起作用(至少在2012 R2上不起作用)。

通过简单地将字体文件复制到C:\Windows\Fonts(在本例中为times.ttf)然后使用PowerShell添加相应的注册表项,我获得了成功:

New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts' -Name 'Times New Roman (TrueType)' -PropertyType String -Value times.ttf

拆卸与安装相反。唯一的缺点是在安装字体后以及在应用程序引用它之前卸载它之前都需要重新启动。

答案 3 :(得分:0)

已知Shell代码在Remote和Build代理上失败 - 如果使用shell的comobjects失败并且您正在通过Remote或Build代理进行审查,那么您将需要使用框架类来执行此操作(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
        }
#>
相关问题