Installing System Font with Powershell

前端 未结 4 1325
悲哀的现实
悲哀的现实 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: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
            }
    #>
    

提交回复
热议问题