From c22bc552221bfa6c2f19e69edaaf2fc860f6defb Mon Sep 17 00:00:00 2001 From: Michael Jahn <47208217+MichaelJahn2@users.noreply.github.com> Date: Wed, 16 Feb 2022 22:56:55 +0100 Subject: [PATCH 1/3] Improve code for InstallFont.ps but try to keep same structure and functionality as MickIT original. Prepare code for non-admin installation of fonts --- InstallFonts.ps1 | 59 ++++++++++++++++++++---------------------------- 1 file changed, 25 insertions(+), 34 deletions(-) diff --git a/InstallFonts.ps1 b/InstallFonts.ps1 index 275c029..da1be59 100644 --- a/InstallFonts.ps1 +++ b/InstallFonts.ps1 @@ -46,53 +46,44 @@ function Install-Font { ".ttf" {$FontName = $FontName + [char]32 + '(TrueType)'} ".otf" {$FontName = $FontName + [char]32 + '(OpenType)'} } + $fontTarget = $env:windir + "\Fonts\" + $FontFile.Name + $regPath = "HKLM:\Software\Microsoft\Windows NT\CurrentVersion\Fonts" + $regValue = $FontFile.Name + $regName = $FontName + $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 ("Copying $($FontFile.Name).....") -NoNewline + Copy-Item -Path $fontFile.FullName -Destination ($fontTarget) -Force + # Test if font is copied over + If ((Test-Path ($fontTarget)) -eq $true) { Write-Host ('Success') -Foreground Yellow } else { - Write-Host ('Failed') -ForegroundColor Red + Write-Host ('Failed to copy file') -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 - } + + # Create Registry item for font + Write-Host ("Adding $FontName to the registry.....") -NoNewline + If (!(Test-Path $regPath)) { + New-Item -Path $regPath -Force | Out-Null + } + New-ItemProperty -Path $regPath -Name $regName -Value $regValue -PropertyType string -Force -ErrorAction SilentlyContinue| Out-Null + + $AddKey = $true + If ((Get-ItemPropertyValue -Name $regName -Path $regPath) -eq $regValue) { + Write-Host ('Success') -ForegroundColor Yellow } 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 + Write-Host ('Failed to set registry key') -ForegroundColor Red } + $AddKey = $false } catch { If ($Copy -eq $true) { - Write-Host ('Failed') -ForegroundColor Red + Write-Host ('Font file copy Failed') -ForegroundColor Red $Copy = $false } If ($AddKey -eq $true) { - Write-Host ('Failed') -ForegroundColor Red + Write-Host ('Registry Key Creation Failed') -ForegroundColor Red $AddKey = $false } write-warning $_.exception.message From 88683b4f009c50503b6df8f2e8e6e5c5f084fe15 Mon Sep 17 00:00:00 2001 From: Michael Jahn <47208217+MichaelJahn2@users.noreply.github.com> Date: Tue, 5 Apr 2022 21:14:40 +0200 Subject: [PATCH 2/3] Add check for minimum Win version for local user installation without admin rights. Document parameters --- InstallFonts.ps1 | 47 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/InstallFonts.ps1 b/InstallFonts.ps1 index da1be59..3e9ff4d 100644 --- a/InstallFonts.ps1 +++ b/InstallFonts.ps1 @@ -23,9 +23,12 @@ .PARAMETER FontFile Name of the Font File to install + + .PARAMETER InstallSystemWide + $true for system-wide installation, $false for installation in the local user profile - which does not require admin rights. Requires Windows 10 with at least 17704. .EXAMPLE - PS C:\> Install-Font -FontFile $value1 + PS C:\> Install-Font -FontFile $value1 $true .NOTES Additional information about the function. @@ -33,7 +36,8 @@ function Install-Font { param ( - [Parameter(Mandatory = $true)][ValidateNotNullOrEmpty()][System.IO.FileInfo]$FontFile + [Parameter(Mandatory)][ValidateNotNullOrEmpty()][System.IO.FileInfo]$FontFile, + [Parameter(Mandatory)][Boolean]$InstallSystemWide ) #Get Font Name from the File's Extended Attributes @@ -46,12 +50,25 @@ function Install-Font { ".ttf" {$FontName = $FontName + [char]32 + '(TrueType)'} ".otf" {$FontName = $FontName + [char]32 + '(OpenType)'} } - $fontTarget = $env:windir + "\Fonts\" + $FontFile.Name - $regPath = "HKLM:\Software\Microsoft\Windows NT\CurrentVersion\Fonts" - $regValue = $FontFile.Name - $regName = $FontName + if ($InstallSystemWide) { + $fontTarget = $env:windir + "\Fonts\" + $FontFile.Name + $regPath = "HKLM:\Software\Microsoft\Windows NT\CurrentVersion\Fonts" + $regValue = $FontFile.Name + $regName = $FontName + } else { + # Check whether Windows Version is high enough to support per-user font installation without admin rights + $winMajorVersion = [Environment]::OSVersion.Version.Major + $winBuild = [Environment]::OSVersion.Version.Build + If ( -not (($winMajorVersion -ge 10) -and ($winBuild -ge 17044))) { + throw "At least Windows 10 Build 17044 is required for local user installation. You have Win $winMajorVersion Build $winBuild." + } + $fontTarget = $env:localappdata + "\Microsoft\Windows\Fonts\" + $FontFile.Name + $regPath = "HKCU:\Software\Microsoft\Windows NT\CurrentVersion\Fonts" + $regValue = $fontTarget + $regName = $FontName + } - $Copy = $true + $CopyFailed = $true Write-Host ("Copying $($FontFile.Name).....") -NoNewline Copy-Item -Path $fontFile.FullName -Destination ($fontTarget) -Force # Test if font is copied over @@ -60,7 +77,7 @@ function Install-Font { } else { Write-Host ('Failed to copy file') -ForegroundColor Red } - $Copy = $false + $CopyFailed = $false # Create Registry item for font Write-Host ("Adding $FontName to the registry.....") -NoNewline @@ -69,22 +86,22 @@ function Install-Font { } New-ItemProperty -Path $regPath -Name $regName -Value $regValue -PropertyType string -Force -ErrorAction SilentlyContinue| Out-Null - $AddKey = $true + $AddKeyFailed = $true If ((Get-ItemPropertyValue -Name $regName -Path $regPath) -eq $regValue) { Write-Host ('Success') -ForegroundColor Yellow } else { Write-Host ('Failed to set registry key') -ForegroundColor Red } - $AddKey = $false + $AddKeyFailed = $false } catch { - If ($Copy -eq $true) { + If ($CopyFailed -eq $true) { Write-Host ('Font file copy Failed') -ForegroundColor Red - $Copy = $false + $CopyFailed = $false } - If ($AddKey -eq $true) { + If ($AddKeyFailed -eq $true) { Write-Host ('Registry Key Creation Failed') -ForegroundColor Red - $AddKey = $false + $AddKeyFailed = $false } write-warning $_.exception.message } @@ -95,5 +112,5 @@ function Install-Font { foreach ($FontItem in (Get-ChildItem -Path $PSScriptRoot | Where-Object { ($_.Name -like '*.ttf') -or ($_.Name -like '*.OTF') })) { - Install-Font -FontFile $FontItem + Install-Font -FontFile $FontItem $true } From 30adc25579a31cef0d2a4ed582b40db3cff7992f Mon Sep 17 00:00:00 2001 From: Michael Jahn <47208217+MichaelJahn2@users.noreply.github.com> Date: Wed, 6 Apr 2022 20:19:51 +0200 Subject: [PATCH 3/3] Create per-user Fonts directory if it does not already exist --- InstallFonts.ps1 | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/InstallFonts.ps1 b/InstallFonts.ps1 index 3e9ff4d..137e37e 100644 --- a/InstallFonts.ps1 +++ b/InstallFonts.ps1 @@ -62,10 +62,12 @@ function Install-Font { If ( -not (($winMajorVersion -ge 10) -and ($winBuild -ge 17044))) { throw "At least Windows 10 Build 17044 is required for local user installation. You have Win $winMajorVersion Build $winBuild." } - $fontTarget = $env:localappdata + "\Microsoft\Windows\Fonts\" + $FontFile.Name + $fontTarget = $env:localappdata + "\Microsoft\Windows\Fonts\" $regPath = "HKCU:\Software\Microsoft\Windows NT\CurrentVersion\Fonts" - $regValue = $fontTarget + $regValue = $fontTarget + $FontFile.Name $regName = $FontName + # The Fonts directory does not always exist on a per user level. Create it. + New-Item -ItemType Directory -Force -Path "$fontTarget" } $CopyFailed = $true @@ -112,5 +114,5 @@ function Install-Font { foreach ($FontItem in (Get-ChildItem -Path $PSScriptRoot | Where-Object { ($_.Name -like '*.ttf') -or ($_.Name -like '*.OTF') })) { - Install-Font -FontFile $FontItem $true + Install-Font -FontFile $FontItem $false }