使用Powershell安装系统字体

问题描述 投票:10回答:4

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

powershell windows-server-2008 true-type-fonts
4个回答
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


2
投票

只是想发布一个不需要0x14硬编码到脚本中的替代方案。将文件对象传递给函数,它将根据文件的位置运行“Install”:

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")
   }
}

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

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


0
投票

已知Shell代码在远程和构建代理上失败 - 如果使用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
        }
#>
© www.soinside.com 2019 - 2024. All rights reserved.