diff --git a/InstallFonts.ps1 b/InstallFonts.ps1 index 275c029..137e37e 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,54 +50,60 @@ function Install-Font { ".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) { + 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\" + $regPath = "HKCU:\Software\Microsoft\Windows NT\CurrentVersion\Fonts" + $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 + 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 - } + $CopyFailed = $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 + + $AddKeyFailed = $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 } + $AddKeyFailed = $false } catch { - If ($Copy -eq $true) { - Write-Host ('Failed') -ForegroundColor Red - $Copy = $false + If ($CopyFailed -eq $true) { + Write-Host ('Font file copy Failed') -ForegroundColor Red + $CopyFailed = $false } - If ($AddKey -eq $true) { - Write-Host ('Failed') -ForegroundColor Red - $AddKey = $false + If ($AddKeyFailed -eq $true) { + Write-Host ('Registry Key Creation Failed') -ForegroundColor Red + $AddKeyFailed = $false } write-warning $_.exception.message } @@ -104,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 + Install-Font -FontFile $FontItem $false }