Installing System Font with Powershell

前端 未结 4 1243
悲哀的现实
悲哀的现实 2021-02-07 23:51

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

相关标签:
4条回答
  • 2021-02-08 00:18

    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")
       }
    }
    
    0 讨论(0)
  • 2021-02-08 00:30

    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

    0 讨论(0)
  • 2021-02-08 00:37

    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.

    0 讨论(0)
  • 2021-02-08 00:40

    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
            }
    #>
    
    0 讨论(0)
提交回复
热议问题