You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello everyone,
I just made the mistake to install almost all of the fonts on my windows machine by accident. I actually wanted to just install two fonts 😅. Should've read the docs. Wow.
Anyhow, after I realized, that there isn't an uninstall script for Windows, I've puzzled together the following script on my own.
Disclaimer: I've never written PowerShell before, so please be keep that in mind before running it on your own machine😬
functionUninstall-Font {
[CmdletBinding()]
param(
[parameter(mandatory=$true,ValueFromPipeline=$true)][ValidateNotNullOrEmpty()][string[]]$fontname
)
Begin {
# Add functions and assembliesif (!("gdi.font"-as [type])){
Add-Type-AssemblyName System.Drawing
Add-Type-MemberDefinition ' [DllImport("user32.dll")] public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, String lParam); [DllImport("gdi32.dll")] public static extern int RemoveFontResource(string lpszFilename);'-Namespace gdi -Name font -EA Stop
}
# Get installed font collection$installedfonts= (New-Object System.Drawing.Text.InstalledFontCollection).Families
$myWindowsID= [System.Security.Principal.WindowsIdentity]::GetCurrent()
$myWindowsPrincipal=new-object System.Security.Principal.WindowsPrincipal($myWindowsID)
$adminRole=[System.Security.Principal.WindowsBuiltInRole]::Administrator
$UserisAdmin=$myWindowsPrincipal.IsInRole($adminRole)
$keyuser=get-item'HKCU:\Software\Microsoft\Windows NT\CurrentVersion\Fonts'-EA SilentlyContinue
$keymachine=get-item'HKLM:\Software\Microsoft\Windows NT\CurrentVersion\Fonts'-EA SilentlyContinue
}
Process{
foreach ($fontin$fontname){
if ($font-notin$installedfonts.Name){
write-Error"Font '$font' is not installed."-Category InvalidArgument
continue
}
if ($keyuser){
$keyuser.Property|?{$_-like"$font*"} |%{
$file=$keyuser.GetValue($_)
# remove open font resourceswrite-verbose"Removing open resources for Font '$_'"
[gdi.font]::RemoveFontResource($file) |out-null# remove registry valuewrite-verbose"Removing font registry entry with name '$_'."Remove-ItemProperty$keyuser.PSPath-Name $_# Remove font filewrite-verbose"Removing font file '$file'."remove-item$file-Force
}
}
if ($keymachine){
$keymachine.Property|?{$_-like"$font*"} |%{
if (!$UserisAdmin){
write-error"Function needs to run elevated to uninstall the font '$_' from the system!"-Category PermissionDenied
continue
}
$file=$keymachine.GetValue($_)
# remove open font resourceswrite-verbose"Removing open resources for Font '$_'."
[gdi.font]::RemoveFontResource($file) |out-null# remove registry valuewrite-verbose"Removing font registry entry with name '$_'."Remove-ItemProperty$keymachine.PSPath-Name $_# Remove font filewrite-verbose"Removing font file '$file'."remove-item"$([System.Environment]::GetFolderPath(20))\$file"-Force
}
}
}
}
End {
# inform all top level windows of font-changewrite-verbose"Notify all top level windows of font-change with broadcast message."
[gdi.font]::SendMessage(0xffff,0x001D,[IntPtr]::Zero,[IntPtr]::Zero) |out-null
}
}
# Get the name of a font by filefunctionGet-FontNameFromFile([string]$file){
Add-Type-A System.Drawing
$fc=New-Object System.Drawing.Text.PrivateFontCollection
$fc.AddFontFile($file)
return$fc.Families[0].Name
}
# Select all fonts$FontName='*'# Get all fonts in $FontName $fontFiles=New-Object'System.Collections.Generic.List[System.IO.FileInfo]'foreach ($aFontNamein$FontName) {
Get-ChildItem$PSScriptRoot-Filter "${aFontName}.ttf"-Recurse |Foreach-Object {$fontFiles.Add($_)}
Get-ChildItem$PSScriptRoot-Filter "${aFontName}.otf"-Recurse |Foreach-Object {$fontFiles.Add($_)}
}
# Uninstall the fontsforeach ($fontFilein$fontFiles) {
$fontName=Get-FontNameFromFile$fontFile.FullNameUninstall-Font-FontName $fontName-Verbose
}
The script only kind of works and I would love to get some input/feedback.
Bugs:
Right now, I do get a lot of Uninstall-Font : Font '********* ' is not installed., but that's probably, because I stopped the install script halfway, so not all fonts are installed.
Not all fonts are getting uninstalled. When I go to the Windows 10 font settings page there are still a lot of "Powerline" fonts there. Can anybody tell me why?
Cheers,
Tom
The text was updated successfully, but these errors were encountered:
Hello everyone,
I just made the mistake to install almost all of the fonts on my windows machine by accident. I actually wanted to just install two fonts 😅. Should've read the docs. Wow.
Anyhow, after I realized, that there isn't an uninstall script for Windows, I've puzzled together the following script on my own.
Disclaimer: I've never written PowerShell before, so please be keep that in mind before running it on your own machine😬
The script only kind of works and I would love to get some input/feedback.
Bugs:
Right now, I do get a lot of
Uninstall-Font : Font '********* ' is not installed.
, but that's probably, because I stopped the install script halfway, so not all fonts are installed.Not all fonts are getting uninstalled. When I go to the Windows 10 font settings page there are still a lot of "Powerline" fonts there. Can anybody tell me why?
Cheers,
Tom
The text was updated successfully, but these errors were encountered: