Tuesday, November 26, 2019

Installing the Cascadia Code Font (using PowerShell)

In a recent article, I wrote about the new font Microsoft has created, Cascadia Code. I just love the new font and am using it with PowerShell, VS Code and more. I also use it in a variety of VMs, so automating the download and installation is important.

It turns out that downloading and installing a new font is relatively straightforward, like this:

# Install Cascadia Code
# 1. Download Cascadia Code font from GitHub
$DLPath = 'https://github.com/microsoft/cascadia-code/releases/'+
          'download/v1911.20/Cascadia.ttf'
$DLFile = 'C:\Foo\Cascadia.TTF'
Invoke-WebRequest -Uri $DLPath -OutFile $DLFile

# 2. Now Install the Font 
$Font = New-Object -Com Shell.Application
$Destination = (New-Object -ComObject Shell.Application).Namespace(0x14)
$Destination.CopyHere($DLFile,0x10)

Simples as they say.

One thing - the URL download path is hardcoded. You may need to adjust it going forward. Maybe someone can show me how to work out the latest version programmatically.

[Later]
I noticed that Blogger' editor had mangled the code - sorry but thanks for the heads up.

3 comments:

  1. Thanks for the post ... useful

    Some niggles ...
    - formatting would help

    For me, the Invoke-WebRequest fails because of SSL/TLS, which needed the fix suggested on StackOverflow ... https://stackoverflow.com/questions/41618766/powershell-invoke-webrequest-fails-with-ssl-tls-secure-channel

    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

    ReplyDelete
  2. Since the latest release version would be listed first in a list of download links you could pull the links from the page that include the name of the version of the file you want to download, like so:

    #Set Cascadia Code font variables
    $cascadiaFont = "CascadiaPL.ttf"
    $cascadiaReleasesURL = "https://github.com/microsoft/cascadia-code/releases"
    $cascadiaReleases = Invoke-WebRequest -Uri $cascadiaReleasesURL
    $cascadiaPath = "https://github.com" + ($cascadiaReleases.Links.href | Where-Object { $_ -match "($cascadiaFont)" } | Select-Object -First 1)
    $cascadiaFile = "C:\REPOS\$cascadiaFont"
    # Download Cascadia Code
    Invoke-WebRequest -Uri $cascadiaPath -OutFile $cascadiaFile

    # Install Cascadia Code
    $fontShellApp = New-Object -Com Shell.Application
    $fontShellNamespace = (New-Object -ComObject Shell.Application).Namespace(0x14)
    $fontShellNamespace.CopyHere($cascadiaFile, 0x10)

    ReplyDelete
  3. @Grey Wall IT

    THANK YOU!

    That is much better code - I just am not all that good with screencraping, so thanks for the code.

    ReplyDelete