Skip to content

Commit 48764db

Browse files
committedDec 22, 2023
Uninstall font
1 parent e7f52dd commit 48764db

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed
 

‎Windows/Fonts/Uninstall-Font.ps1

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
. "$PSScriptRoot\Get-Font.ps1"
2+
3+
function Uninstall-Font {
4+
[CmdletBinding(SupportsShouldProcess = $true)]
5+
param
6+
(
7+
[Alias('Name')]
8+
[ValidateNotNullOrEmpty()]
9+
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
10+
[string] $FontName,
11+
12+
[ValidateSet('User', 'Machine')]
13+
[Parameter(Mandatory = $true)]
14+
[string]$Type
15+
)
16+
begin {
17+
[bool] $isUser = $Type -eq 'User'
18+
[string] $drive = $isUser ? 'HKCU:' : 'HKLM:'
19+
[string] $installFolder = $isUser ? "${env:LOCALAPPDATA}\Microsoft\Windows\Fonts" : "${env:windir}\Fonts\"
20+
[string] $registryPath = "$drive\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts"
21+
22+
@('ErrorAction', 'Type', 'Clobber').ForEach( { $null = $PSBoundParameters.Remove($_) })
23+
}
24+
process {
25+
$PsBoundParameters['Verbose'] = $false
26+
$PsBoundParameters['ErrorAction'] = 'SilentlyContinue'
27+
28+
$null = $PSBoundParameters.Remove('FontName')
29+
30+
$fonts = Get-Font -Name $FontName -Type $Type
31+
32+
if ($PSCmdlet.ShouldProcess($fontName, 'Uninstall font')) {
33+
foreach ($font in $fonts) {
34+
$fontPath = $font.FileName
35+
36+
if (Test-Path $fontPath) {
37+
Remove-Item -LiteralPath $fontPath -Force @PSBoundParameters -WhatIf:$false
38+
39+
if ($?) {
40+
$removeItemPropertySplat = @{
41+
Name = $fontName
42+
Path = $registryPath
43+
Force = $true
44+
}
45+
Remove-ItemProperty @removeItemPropertySplat @PSBoundParameters -WhatIf:$false | Out-Null
46+
}
47+
} else {
48+
Write-Warning "Font $fontName is not installed: file ($fontPath) does not exist!"
49+
}
50+
}
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)
Failed to load comments.