powershellinstallationfontspowershell-3.0

A PowerShell Script to install fonts on Windows 11


I would like help with the following ps script that I took from reddit to install fonts for all Windows 11 users, below is the original script:

#set font source location

$FontFolder = "FONT LOCATION"

$FontItem = Get-Item -Path $FontFolder

#go through all folders in source and list all fon, otf, ttc and ttf files

$FontList = Get-ChildItem -Path "$FontItem\*" -Include ('*.fon','*.otf','*.ttc','*.ttf') -Recurse

foreach ($Font in $FontList) {

Write-Host 'Installing font -' $Font.BaseName

Copy-Item $Font "C:\Windows\Fonts"

#register font for all users

New-ItemProperty -Name $Font.BaseName -Path "HKLM:\Software\Microsoft\Windows NT\CurrentVersion\Fonts" -PropertyType string -Value $Font.name

}

In the original script I need to set the full path of the folder where the fonts are, and if I copy this folder to another drive I have to update the path before running it, but what I need and want is for it to recognize the folder where it is being executed without me having to set the path, as happens with a batch script that starts with the command cd /d "%~dp0", like the one below that I use to install the Notepad++:

mode con lines=30 cols=70
@ECHO off
cd /d "%~dp0"
cd /d "%~dp0" && ( if exist "%temp%\getadmin.vbs" del "%temp%\getadmin.vbs" ) && fsutil dirty query %systemdrive% 1>nul 2>nul || (  echo Set UAC = CreateObject^("Shell.Application"^) : UAC.ShellExecute "cmd.exe", "/k cd ""%~sdp0"" && %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs" && "%temp%\getadmin.vbs" && exit /B )
cls
echo.
@echo  ßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßß
@echo   ßß                                                              ßß
@echo    ßß           S O F T E H A R D    S O L U T I O N S           ßß
@echo     ßß                                                          ßß
@echo      ßß       ßßßß    ßßßß     ßß  ßß      ßßßßßß  ßßßßßß      ßß
@echo       ßß     ßß  ßß  ßß  ßß    ßß  ßßß     ßß      ßß  ßß     ßß
@echo        ßß    ßß      ßß        ßß  ßß ßß   ßß      ßß  ßß    ßß
@echo         ßß   ßßßß    ßßßß      ßß  ßß  ßß  ßßßßßß  ßß  ßß   ßß
@echo         ßß     ßßßß    ßßßß    ßß  ßß  ßß  ßß      ßß  ßß   ßß
@echo        ßß        ßß      ßß    ßß  ßß  ßß  ßß      ßß  ßß    ßß
@echo       ßß     ßß  ßß  ßß  ßß    ßß  ßß  ßß  ßß      ßß  ßß     ßß
@echo      ßß       ßßßß    ßßßß     ßß  ßß  ßß  ßß      ßßßßßß      ßß
@echo     ßß                                                          ßß
@echo    ßß                    I N F O R M A T I C A                   ßß
@echo   ßß                                                              ßß
@echo  ßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßß
@CHCP 1252 >NUL                                                            
@Title Silent installation of Notepad++ software version 64-bit..          
echo.                                                                      
echo.         We are starting the silent installation of the software      
echo.                                                                      
echo.                          "Notepad++ 64-bit"                          
echo.                                                                      
echo.                   When the installation is complete                  
echo.             this message will disappear and the application          
echo.             will start so you can configure it before using it.      
echo.                                                                      
@echo OFF
FOR %%i IN ("npp*.exe") DO Set FileName="%%i"
%FileName% /S
echo. Installation completed successfully - Starting Notepad Plus Plus 64-bit!
@Start explorer.exe "%ProgramData%\Microsoft\Windows\Start Menu\Programs\Notepad++.lnk"
timeout /t 4
exit

I search for the equivalent "cd /d "%~dp0"" command and I just found this "$PSScriptRoot", did anyone knows if this will solve my problem?

Thanks in advance for any help.

Best Regards


Solution

  • As you correctly stated the variable $PSScriptRoot is exactly what you need

    $FontList = Get-ChildItem -Path $PSScriptRoot -Include ('*.fon','*.otf','*.ttc','*.ttf') -Recurse
    

    Beaware that the variable $PSScriptRoot is not populated in the following scenarios: