通过 PowerShell 获取已卸载 Windows 字体的详细信息
Get details of uninstalled Windows fonts via PowerShell
我正在使用 PowerShell 和 ImageMagick 创建我的字体的自定义图像 collection 以确定我要将哪些字体安装到系统中。我想让图像包含一些字体信息,但我很难提取这些信息。我在 https://powershell.org/forums/topic/listing-font-details/#post-78006 找到了一些有用的代码,但我发现只能在 系统字体目录 中访问这些信息。我的 collection 存在于 %SystemRoot%\Fonts
.
的 之外
我想访问
- 字体名称 (Aparajita)
- 字体样式(粗体、普通、斜体)
- 字体类型(光栅,OpenType/TrueType)
等等
这可能吗?
使用 Shell.Application
COM 对象获取文件的详细信息不限于 %SystemRoot%\Fonts
文件夹。
如果您在磁盘上有一个文件夹用于保存字体集,您可以使用以下代码获取每个字体文件的信息:
function Get-FontInfo {
[CmdletBinding()]
[OutputType([Psobject])]
Param (
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
[string]$SourceFolder,
[switch]$Recurse
)
# using a lookup hashtable to avoid localized field names
$fontProperties = [ordered]@{
0 = 'Name'
1 = 'Size'
2 = 'Type'
20 = 'Author'
21 = 'Title'
25 = 'Copyright'
33 = 'Company'
34 = 'Description'
164 = 'Extension'
165 = 'FileName'
166 = 'Version'
194 = 'Path'
196 = 'FileType'
310 = 'Trademark'
}
$shell = New-Object -ComObject "Shell.Application"
$objDir = $shell.NameSpace($SourceFolder)
$files = Get-ChildItem -Path $SourceFolder -Filter '*.*' -File -Recurse:$Recurse
foreach($file in $files) {
$objFile = $objDir.ParseName($file.Name)
$mediaFile = $objDir.Items()
$output = [ordered]@{}
$fontProperties.GetEnumerator() | ForEach-Object {
$name = $objDir.GetDetailsOf($mediaFile, $_.Name)
if (![string]::IsNullOrWhiteSpace($name)) {
$output[$_.Value] = $objDir.GetDetailsOf($objFile, $_.Name)
}
}
[PsCustomObject]$output
}
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($objFile)
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($objDir)
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($shell)
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
}
Get-FontInfo -SourceFolder 'D:\Test' # the path of the font folder
如果源文件夹中有子文件夹,还要添加 -Recurse
开关。
输出如下:
Name : aparaj.ttf
Size : 194 kB
Type : TrueType-lettertypebestand
Author :
Title : Aparajita
Copyright : Copyright (c) 2011, Modular Infotech, Pune, INDIA. - Licenced to Microsoft
Company :
Description : This font is primarily meant for use in displaying Hindi text in documents. It is an OpenType font, based on Unicode.
Extension : .ttf
FileName : aparaj.ttf
Version : 6.00
Path : D:\Test\aparaj.ttf
FileType : TrueType-lettertypebestand
Trademark :
在查看 Theo 的回答并进行了一些代码修改后,我发现根据是否安装字体,存在不同的扩展属性。
以下输出的顶部是安装字体时的情况,而底部是未安装时的情况。简而言之,如果我想将 'Font Style' 和 'Designed For' 嵌入到我生成的图像中,我需要先安装字体。
Value Attribute Index
----- --------- -----
Regular Font style 1
Show Show/hide 2
Arabic Designed for 3
Text Category 4
Microsoft Corporation Designer/foundry 5
Editable Font embeddability 6
OpenType Font type 7
Arabic Typesetting Family 8
2013-Aug-22 11:34 A Date modified 10
505 KB Size 11
C:\Windows\Fonts\ARABTYPE.TTF Font file names 13
6.00 Font version 14
------------------------------------------------------------------------------------
609 KB Size 1
TrueType font file Item type 2
2009-Jun-10 04:43 P Date modified 3
2019-Dec-16 01:50 P Date created 4
2019-Dec-16 01:50 P Date accessed 5
A Attributes 6
Available offline Availability 8
Unspecified Perceived type 9
Everyone Owner 10
Unrated Rating 19
Mamoun Sakkal, Paul C. Nelson and John Hudson Authors 20
Arabic Typesetting Title 21
© 2008 Microsoft Corporation. All Rights Res... Copyright 25
94.9 GB Total size 50
WINDOWS8.1 (this PC) Computer 53
.ttf File extension 156
arabtype.ttf Filename 157
5.91 File version 158
1.81 GB Space free 161
No Shared 178
Fonts Folder name 181
Y:\Documents\Fonts Folder path 182
Fonts (Y:\Documents) Folder 183
Y:\Documents\Fonts\arabtype.ttf Path 185
TrueType font file Type 187
Unresolved Link status 193
98% Space used 242
Not shared Sharing status 282
Available <unknown> 283
我正在使用 PowerShell 和 ImageMagick 创建我的字体的自定义图像 collection 以确定我要将哪些字体安装到系统中。我想让图像包含一些字体信息,但我很难提取这些信息。我在 https://powershell.org/forums/topic/listing-font-details/#post-78006 找到了一些有用的代码,但我发现只能在 系统字体目录 中访问这些信息。我的 collection 存在于 %SystemRoot%\Fonts
.
我想访问
- 字体名称 (Aparajita)
- 字体样式(粗体、普通、斜体)
- 字体类型(光栅,OpenType/TrueType)
等等
这可能吗?
使用 Shell.Application
COM 对象获取文件的详细信息不限于 %SystemRoot%\Fonts
文件夹。
如果您在磁盘上有一个文件夹用于保存字体集,您可以使用以下代码获取每个字体文件的信息:
function Get-FontInfo {
[CmdletBinding()]
[OutputType([Psobject])]
Param (
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
[string]$SourceFolder,
[switch]$Recurse
)
# using a lookup hashtable to avoid localized field names
$fontProperties = [ordered]@{
0 = 'Name'
1 = 'Size'
2 = 'Type'
20 = 'Author'
21 = 'Title'
25 = 'Copyright'
33 = 'Company'
34 = 'Description'
164 = 'Extension'
165 = 'FileName'
166 = 'Version'
194 = 'Path'
196 = 'FileType'
310 = 'Trademark'
}
$shell = New-Object -ComObject "Shell.Application"
$objDir = $shell.NameSpace($SourceFolder)
$files = Get-ChildItem -Path $SourceFolder -Filter '*.*' -File -Recurse:$Recurse
foreach($file in $files) {
$objFile = $objDir.ParseName($file.Name)
$mediaFile = $objDir.Items()
$output = [ordered]@{}
$fontProperties.GetEnumerator() | ForEach-Object {
$name = $objDir.GetDetailsOf($mediaFile, $_.Name)
if (![string]::IsNullOrWhiteSpace($name)) {
$output[$_.Value] = $objDir.GetDetailsOf($objFile, $_.Name)
}
}
[PsCustomObject]$output
}
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($objFile)
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($objDir)
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($shell)
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
}
Get-FontInfo -SourceFolder 'D:\Test' # the path of the font folder
如果源文件夹中有子文件夹,还要添加 -Recurse
开关。
输出如下:
Name : aparaj.ttf Size : 194 kB Type : TrueType-lettertypebestand Author : Title : Aparajita Copyright : Copyright (c) 2011, Modular Infotech, Pune, INDIA. - Licenced to Microsoft Company : Description : This font is primarily meant for use in displaying Hindi text in documents. It is an OpenType font, based on Unicode. Extension : .ttf FileName : aparaj.ttf Version : 6.00 Path : D:\Test\aparaj.ttf FileType : TrueType-lettertypebestand Trademark :
在查看 Theo 的回答并进行了一些代码修改后,我发现根据是否安装字体,存在不同的扩展属性。 以下输出的顶部是安装字体时的情况,而底部是未安装时的情况。简而言之,如果我想将 'Font Style' 和 'Designed For' 嵌入到我生成的图像中,我需要先安装字体。
Value Attribute Index
----- --------- -----
Regular Font style 1
Show Show/hide 2
Arabic Designed for 3
Text Category 4
Microsoft Corporation Designer/foundry 5
Editable Font embeddability 6
OpenType Font type 7
Arabic Typesetting Family 8
2013-Aug-22 11:34 A Date modified 10
505 KB Size 11
C:\Windows\Fonts\ARABTYPE.TTF Font file names 13
6.00 Font version 14
------------------------------------------------------------------------------------
609 KB Size 1
TrueType font file Item type 2
2009-Jun-10 04:43 P Date modified 3
2019-Dec-16 01:50 P Date created 4
2019-Dec-16 01:50 P Date accessed 5
A Attributes 6
Available offline Availability 8
Unspecified Perceived type 9
Everyone Owner 10
Unrated Rating 19
Mamoun Sakkal, Paul C. Nelson and John Hudson Authors 20
Arabic Typesetting Title 21
© 2008 Microsoft Corporation. All Rights Res... Copyright 25
94.9 GB Total size 50
WINDOWS8.1 (this PC) Computer 53
.ttf File extension 156
arabtype.ttf Filename 157
5.91 File version 158
1.81 GB Space free 161
No Shared 178
Fonts Folder name 181
Y:\Documents\Fonts Folder path 182
Fonts (Y:\Documents) Folder 183
Y:\Documents\Fonts\arabtype.ttf Path 185
TrueType font file Type 187
Unresolved Link status 193
98% Space used 242
Not shared Sharing status 282
Available <unknown> 283