powershell安装字体
参考 Install-Fonts (powershellweekly.com)
使用 -File 开关 Install-Fonts -File “c:\Fonts\FontName.ttf”选择一个文件
使用 -Files 开关安装字体 -Files “c:\Fonts” 选择整个文件夹。
<# .Synopsis Install-Fonts Author: Michael J. Thomas Created: 07/10/2019 Updated: 07/10/2019 .DESCRIPTION Install Fonts on a computer. .EXAMPLE Install-Fonts -Files "C:\Fonts" .EXAMPLE Install-Fonts -File "C:\Fonts\LeanStatus.ttf" #> function Install-Fonts { [CmdletBinding()] Param ( [string[]]$Files, [string]$File ) $objShell = New-Object -ComObject Shell.Application $Fonts = $objShell.NameSpace(20) If (!($Files -eq $null)){ Get-ChildItem "$Files\*.ttf" | ForEach-Object {$Fonts.CopyHere($_.FullName)} } ElseIf (!($File -eq $null)){ $Fonts.CopyHere($File) } }
这个是为当前用户安装,如果要给所有用户安装, 参考 使用 PowerShell | 安装字体烷烃 (alkanesolutions.co.uk)
本质是
将字体复制到:
并在其中注册一个条目:
function Install-Font { param ( [System.IO.FileInfo]$fontFile ) try { #get font name $gt = [Windows.Media.GlyphTypeface]::new($fontFile.FullName) $family = $gt.Win32FamilyNames['en-us'] if ($null -eq $family) { $family = $gt.Win32FamilyNames.Values.Item(0) } $face = $gt.Win32FaceNames['en-us'] if ($null -eq $face) { $face = $gt.Win32FaceNames.Values.Item(0) } $fontName = ("$family $face").Trim() switch ($fontFile.Extension) { ".ttf" {$fontName = "$fontName (TrueType)"} ".otf" {$fontName = "$fontName (OpenType)"} } write-host "Installing font: $fontFile with font name '$fontName'" If (!(Test-Path ("$($env:windir)\Fonts\" + $fontFile.Name))) { write-host "Copying font: $fontFile" Copy-Item -Path $fontFile.FullName -Destination ("$($env:windir)\Fonts\" + $fontFile.Name) -Force } else { write-host "Font already exists: $fontFile" } If (!(Get-ItemProperty -Name $fontName -Path "HKLM:\Software\Microsoft\Windows NT\CurrentVersion\Fonts" -ErrorAction SilentlyContinue)) { write-host "Registering font: $fontFile" New-ItemProperty -Name $fontName -Path "HKLM:\Software\Microsoft\Windows NT\CurrentVersion\Fonts" -PropertyType string -Value $fontFile.Name -Force -ErrorAction SilentlyContinue | Out-Null } else { write-host "Font already registered: $fontFile" } [System.Runtime.Interopservices.Marshal]::ReleaseComObject($oShell) | out-null Remove-Variable oShell } catch { write-host "Error installing font: $fontFile. " $_.exception.message } } function Uninstall-Font { param ( [System.IO.FileInfo]$fontFile ) try { #get font name $gt = [Windows.Media.GlyphTypeface]::new($fontFile.FullName) $family = $gt.Win32FamilyNames['en-us'] if ($null -eq $family) { $family = $gt.Win32FamilyNames.Values.Item(0) } $face = $gt.Win32FaceNames['en-us'] if ($null -eq $face) { $face = $gt.Win32FaceNames.Values.Item(0) } $fontName = ("$family $face").Trim() switch ($fontFile.Extension) { ".ttf" {$fontName = "$fontName (TrueType)"} ".otf" {$fontName = "$fontName (OpenType)"} } write-host "Uninstalling font: $fontFile with font name '$fontName'" If (Test-Path ("$($env:windir)\Fonts\" + $fontFile.Name)) { write-host "Removing font: $fontFile" Remove-Item -Path "$($env:windir)\Fonts\$($fontFile.Name)" -Force } else { write-host "Font does not exist: $fontFile" } If (Get-ItemProperty -Name $fontName -Path "HKLM:\Software\Microsoft\Windows NT\CurrentVersion\Fonts" -ErrorAction SilentlyContinue) { write-host "Unregistering font: $fontFile" Remove-ItemProperty -Name $fontName -Path "HKLM:\Software\Microsoft\Windows NT\CurrentVersion\Fonts" -Force } else { write-host "Font not registered: $fontFile" } [System.Runtime.Interopservices.Marshal]::ReleaseComObject($oShell) | out-null Remove-Variable oShell } catch { write-host "Error uninstalling font: $fontFile. " $_.exception.message } } $currentDirectory = [System.AppDomain]::CurrentDomain.BaseDirectory.TrimEnd('\') if ($currentDirectory -eq $PSHOME.TrimEnd('\')) { $currentDirectory = $PSScriptRoot } #Loop through fonts in the same directory as the script and install/uninstall them foreach ($FontItem in (Get-ChildItem -Path $currentDirectory | Where-Object {($_.Name -like '*.ttf') -or ($_.Name -like '*.otf') })) { Install-Font -fontFile $FontItem.FullName }