Problem:
Example: You (Or other users) would like to install fonts to the C:\Windows\Fonts folder (For ALL Users) on desktops as part of the FlexApp process, and you would also like any & all new fonts installed (In-session as well) removed upon user logoff.
Unfortunately, you cannot just delete the fonts in C:\Windows\Fonts to actually get rid of them, as usually you will have Access Denied errors, even when elevated, so we will use the Registry in this case, since it does not require a reboot.
Resolution(s):
In this case, we will use 2 PowerShell example scripts, and a folder full of custom fonts to do this in just a few steps.
1. Create a new FlexApp on your 6.8.5 FPC. Start recording the application.
2. Install the Font/TTF files into your c:\windows\fonts folder manually (Select All font files, Right-Click and select "Install For All Users":
3. Once the font installation has completed, stop the application recording and save the FlexApp.
4. Create two PowerShell scripts for your FlexApp on the FPC.
In our case, the first Pre-Activation script will look something like this, and we'll call it get_new_fonts.ps1. Change "yourserver" to relevant paths for your ENV.
# Set variables for log file locations - We use PS equiv of %username% in the file names:
$logFile = "\\yourserver\Logs\$ENV:USERNAME-Fonts.log"
$initalFontsFile = "\\yourserver\Logs\$ENV:USERNAME-InitialFonts.txt"
## Get all fonts in c:\windows\fonts via registry/HKLM
$CurrentReg = Get-Item -Path "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts" | Select-Object -ExpandProperty Property
# Store results in a file on remote server to refer back to later
$CurrentReg | Out-File -FilePath $initalFontsFile
# Log activity to a file
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$logMessage = "$timestamp - Scanned for Initial fonts. Found $($CurrentReg.Count) installed font(s)."
Add-Content -Path $logFile -Value $LogMessage
Create a second script for Post-Deactivation in your FlexApp on the FPC. Ours will look like this and we'll call it remove_fonts.ps1. Change "yourserver" to relevant paths for your ENV.
# Set variables for log file and input file
$logFile = "\\yourserver\Logs\$ENV:USERNAME-Fonts.log"
$initialFontsFile = "\\yourserver\Logs\$ENV:USERNAME-InitialFonts.txt"
$newFontFile = "\\yourserver\Logs\$ENV:USERNAME-NewFonts.txt"
# Get fonts installed on login
$initialFonts = Get-Content -Path $initialFontsFile
# Get current list of installed fonts
$currentFonts = Get-Item -Path "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts" | Select-Object -ExpandProperty Property
# Compare the two lists to find newly installed fonts
$newFonts = Compare-Object -ReferenceObject $initialFonts -DifferenceObject $currentFonts | Where-Object { $_.SideIndicator -eq "=>" } | Select-Object -ExpandProperty InputObject
# Export the list of newly installed fonts to a text file
$newFonts | Out-File -FilePath $newFontFile
# Remove the fonts from HKLM
Foreach ($newFont in $NewFonts) {
Remove-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts" -Name "$newFont"
Add-Content -Path $logFile -Value "Deleted $newFont Registry Entry from HKLM."
}
# Log the activity to a file
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$logMessage = "$timestamp - Removed $($fontsToRemove.Count) font(s)."
Add-Content -Path $logFile -Value $logMessage
5. On the FPC, "Activate" your new FlexApp Package containing the fonts you installed.
6. Scroll down the list and Click on "Add Scripts":
7. Add get_new_fonts.ps1 as the "Pre Activation" script for the package
8. Add remove_fonts.ps1 as the "Post Deactivation" script for the package
9. Save the package again.
10. Deploy to a VDI desktop and ensure the fonts are installed at login and removed at logoff.
Notes:
We used logging and all file operations on a remote file share in our scripts, using files named by %USERNAME% ($ENV:USERNAME in PowerShell). You can certainly also use local disk like $ENV:TEMP for a location as well.
Related Articles:
How to launch PowerShell scripts with ProfileUnity
Product: ProfileUnity
Product Version: 6.8.5
Expires on: 365 days from publish date
Updated: March 30, 2023