forked from MicksITBlogs/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInstallFonts.ps1
108 lines (98 loc) · 4.21 KB
/
InstallFonts.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<#
.SYNOPSIS
Install Open Text and True Type Fonts
.DESCRIPTION
This script will install OTF and TTF fonts that exist in the same directory as the script.
.NOTES
===========================================================================
Created with: SAPIEN Technologies, Inc., PowerShell Studio 2021 v5.8.187
Created on: 6/24/2021 9:36 AM
Created by: Mick Pletcher
Filename: InstallFonts.ps1
===========================================================================
#>
<#
.SYNOPSIS
Install the font
.DESCRIPTION
This function will attempt to install the font by copying it to the c:\windows\fonts directory and then registering it in the registry. This also outputs the status of each step for easy tracking.
.PARAMETER FontFile
Name of the Font File to install
.EXAMPLE
PS C:\> Install-Font -FontFile $value1
.NOTES
Additional information about the function.
#>
function Install-Font {
param
(
[Parameter(Mandatory = $true)][ValidateNotNullOrEmpty()][System.IO.FileInfo]$FontFile
)
#Get Font Name from the File's Extended Attributes
$oShell = new-object -com shell.application
$Folder = $oShell.namespace($FontFile.DirectoryName)
$Item = $Folder.Items().Item($FontFile.Name)
$FontName = $Folder.GetDetailsOf($Item, 21)
try {
switch ($FontFile.Extension) {
".ttf" {$FontName = $FontName + [char]32 + '(TrueType)'}
".otf" {$FontName = $FontName + [char]32 + '(OpenType)'}
}
$Copy = $true
Write-Host ('Copying' + [char]32 + $FontFile.Name + '.....') -NoNewline
Copy-Item -Path $fontFile.FullName -Destination ("C:\Windows\Fonts\" + $FontFile.Name) -Force
#Test if font is copied over
If ((Test-Path ("C:\Windows\Fonts\" + $FontFile.Name)) -eq $true) {
Write-Host ('Success') -Foreground Yellow
} else {
Write-Host ('Failed') -ForegroundColor Red
}
$Copy = $false
#Test if font registry entry exists
If ((Get-ItemProperty -Name $FontName -Path "HKLM:\Software\Microsoft\Windows NT\CurrentVersion\Fonts" -ErrorAction SilentlyContinue) -ne $null) {
#Test if the entry matches the font file name
If ((Get-ItemPropertyValue -Name $FontName -Path "HKLM:\Software\Microsoft\Windows NT\CurrentVersion\Fonts") -eq $FontFile.Name) {
Write-Host ('Adding' + [char]32 + $FontName + [char]32 + 'to the registry.....') -NoNewline
Write-Host ('Success') -ForegroundColor Yellow
} else {
$AddKey = $true
Remove-ItemProperty -Name $FontName -Path "HKLM:\Software\Microsoft\Windows NT\CurrentVersion\Fonts" -Force
Write-Host ('Adding' + [char]32 + $FontName + [char]32 + 'to the registry.....') -NoNewline
New-ItemProperty -Name $FontName -Path "HKLM:\Software\Microsoft\Windows NT\CurrentVersion\Fonts" -PropertyType string -Value $FontFile.Name -Force -ErrorAction SilentlyContinue | Out-Null
If ((Get-ItemPropertyValue -Name $FontName -Path "HKLM:\Software\Microsoft\Windows NT\CurrentVersion\Fonts") -eq $FontFile.Name) {
Write-Host ('Success') -ForegroundColor Yellow
} else {
Write-Host ('Failed') -ForegroundColor Red
}
$AddKey = $false
}
} else {
$AddKey = $true
Write-Host ('Adding' + [char]32 + $FontName + [char]32 + 'to the registry.....') -NoNewline
New-ItemProperty -Name $FontName -Path "HKLM:\Software\Microsoft\Windows NT\CurrentVersion\Fonts" -PropertyType string -Value $FontFile.Name -Force -ErrorAction SilentlyContinue | Out-Null
If ((Get-ItemPropertyValue -Name $FontName -Path "HKLM:\Software\Microsoft\Windows NT\CurrentVersion\Fonts") -eq $FontFile.Name) {
Write-Host ('Success') -ForegroundColor Yellow
} else {
Write-Host ('Failed') -ForegroundColor Red
}
$AddKey = $false
}
} catch {
If ($Copy -eq $true) {
Write-Host ('Failed') -ForegroundColor Red
$Copy = $false
}
If ($AddKey -eq $true) {
Write-Host ('Failed') -ForegroundColor Red
$AddKey = $false
}
write-warning $_.exception.message
}
Write-Host
}
#Get a list of all font files relative to this script and parse through the list
foreach ($FontItem in (Get-ChildItem -Path $PSScriptRoot | Where-Object {
($_.Name -like '*.ttf') -or ($_.Name -like '*.OTF')
})) {
Install-Font -FontFile $FontItem
}