|
| 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