forked from microsoft/navcontainerhelper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addfonts.ps1
83 lines (72 loc) · 2.91 KB
/
addfonts.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
$fontsFolderPath = "C:\Windows\Fonts"
$ExistingFonts = Get-ChildItem -Path $fontsFolderPath | % { $_.Name }
#*******************************************************************
# Load C# code
#*******************************************************************
$fontCSharpCode = @'
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;
namespace FontResource
{
public class AddRemoveFonts
{
[DllImport("gdi32.dll")]
static extern int AddFontResource(string lpFilename);
public static int AddFont(string fontFilePath) {
try
{
return AddFontResource(fontFilePath);
}
catch
{
return 0;
}
}
}
}
'@
Add-Type $fontCSharpCode
# Create hashtable containing valid font file extensions and text to append to Registry entry name.
$hashFontFileTypes = @{}
$hashFontFileTypes.Add(".fon", "")
$hashFontFileTypes.Add(".fnt", "")
$hashFontFileTypes.Add(".ttf", " (TrueType)")
$hashFontFileTypes.Add(".ttc", " (TrueType)")
$hashFontFileTypes.Add(".otf", " (OpenType)")
$fontRegistryPath = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts"
Get-ChildItem $PSScriptRoot -ErrorAction Ignore | % {
if ($hashFontFileTypes.ContainsKey($_.Extension)) {
$path = Join-Path "c:\Windows\Fonts" $_.Name
if ($ExistingFonts.Contains($_.Name)) {
Write-Host "Skipping font '$path' as it is already installed"
}
else {
Write-Host "Installing font '$path' " -NoNewline
try {
Copy-Item -Path $_.FullName -Destination $path
$fileDir = split-path $path
$fileName = split-path $path -leaf
$fileExt = (Get-Item $path).extension
$fileBaseName = $fileName -replace($fileExt ,"")
$shell = new-object -com shell.application
$myFolder = $shell.Namespace($fileDir)
$fileobj = $myFolder.Items().Item($fileName)
$fontName = $myFolder.GetDetailsOf($fileobj,21)
if ($fontName -eq "") { $fontName = $fileBaseName }
$retVal = [FontResource.AddRemoveFonts]::AddFont($path)
if ($retVal -eq 0) {
Write-Host -ForegroundColor Red "failed"
} else {
Set-ItemProperty -path "$($fontRegistryPath)" -name "$($fontName)$($hashFontFileTypes.item($fileExt))" -value "$($fileName)" -type STRING
Write-Host -ForegroundColor Green "succeeded"
}
}
catch {
Write-Host -ForegroundColor Red "exception: $($_.Exception.Message)"
}
}
}
}