Changing the Fonts Used by the Windows PowerShell ISE

Changing the Fonts Used by the Windows PowerShell ISE

  • Comments 2
  • Likes

 

Summary: See how to change the fonts used by the Windows PowerShell ISE in the Script pane and Output pane.

 

Microsoft Scripting Guy Ed Wilson here. The summer is rapidly drawing to a close, although the summer heat and humidity will more than likely linger for another month. The key indicator that summer is over is the appearance of the ubiquitous yellow school buses that carry children off to school, and the pull behind trailers that seem to accompany college students off to their dorms. The local university seems to attract learners from all over, who flock to town like migratory birds in search of advantageous weather. Soon the plays, concerts, sports events, pageants, and other rites of fall that accompany the students return will appear on the cultural calendars around town, and Charlotte will crawl out from beneath the blanket of oppressive heat, stifling humidity, and occasional smog to receive a new lease on life. Fall, as it turns out, is the best time of the year in Charlotte, and everyone who lives in the area eagerly seeks its arrival.

When it comes to picking out fonts, I can hardly tell an Arial from a Times New Roman. I know I am a “computer person” who is supposed to know “everything about computers” but I don’t –I am sure you get the same treatment I do at the hands of my friends, family and neighbors.

“My computer is acting funny,” sayeth my Octogenarian neighbor.

“Mine too. Why just yesterday, when I logged on I got a dialog box that said ‘What are prehistoric monsters called when they sleep?’ I don’t know I said, wishing I hadn’t.”

“What happened,” asked my neighbor.

“My computer said a sleeping prehistoric monster is called a dinosnore!”

“That’s bad,” he said.

“Yes, I felt like F-disking my computer,” I replied.

“What is a Fdisk,” he asked, “Is that anything like a floppy disk,” he postulated.

“Not exactly,” I said.

“So, in reality your computer is not too funny after all,” he continued with a note of seriousness in his voice.

“You are right,” I said acknowledging my failed attempt at geek-humor.

Two hours later, I finally emerged from my encounter with the neighbor’s funny computer. It turned out that the grandkids had been downloading ad-ware, and he was being constantly harassed by random popup messages and when he attempted to close one add, five more ads would appear.

Anyway, before I can pick out new settings for the Windows PowerShell ISE, I need to know what fonts are available, and how they look. I decided to write a script that retrieves all of the system fonts, and one by one changes the script pane and the command pane to use each of the system fonts. The Get-PsISEfonts.ps1 script is seen here.

Get-PsISEfonts.ps1

Function Get-Fonts
{
Param($fontSize = 16, 
$milliSecs = 750
)
[windows.media.fonts]::systemTypeFaces | 
Select-Object -Property fontFamily -Unique |
ForEach-Object {
$_.fontFamily.source
$psISE.Options.FontName = $_.fontFamily.source
$psISE.Options.FontSize = $fontSize
Start-Sleep -Milliseconds $millisecs }
} #function get-fonts
# *** entrypoint to script ***
$font = $psISE.Options.FontName
$size = $psISE.Options.FontSize
get-fonts -fontSize 14 -milliSecs 500
$psise.options.FontName = $font
$psISE.Options.FontSize = $size

The Get-Fonts function accepts two parameters. The first is fontsize, and the second is the the number of seconds to display the font. By default, the fontsize is set to 16 and the milliseconds to 750. This is seen here.

Function Get-Fonts
{
Param($fontSize = 16, 
$milliSecs = 750
)

Next the static systemTypeFaces property is retrieved from the Windows.Media.Fonts .NET Framework class. This property returns all the system fonts inside the Windows PowerShell ISE, but in the Windows PowerShell console it returns an error because the Windows.Media.Fonts class is not available. The command is seen here.

[windows.media.fonts]::systemTypeFaces

If you look through the output produced by the above command, you will see many instances of what appear to be the same font, but with different configurations. Here is an example of a single entry:

FontFamily : Agency FB
Weight : Normal
Style : Normal
Stretch : Condensed
IsObliqueSimulated : False
IsBoldSimulated : False
XHeight : 0.47509765625
CapsHeight : 0.76416015625
UnderlinePosition : -0.126953125
UnderlineThickness : 0.0498046875
StrikethroughPosition : 0.2587890625
StrikethroughThickness : 0.0498046875
FaceNames : {ca-es, cs-cz, da-dk, de-de...}

I am only interested in unique instances of the fontFamily property. The results of the previous command are pipelined to the select-object command seen here.

Select-Object -Property fontFamily -Unique

For each unique font that is found, I display the font name, and set the Windows PowerShell ISE font to that value, and change the fontsize to the value specified to the fontsize parameter. The Start-Sleep cmdlet is used to pause the script to allow you to see what the font looks like. This portion of the function is seen here.

ForEach-Object {
$_.fontFamily.source
$psISE.Options.FontName = $_.fontFamily.source
$psISE.Options.FontSize = $fontSize
Start-Sleep -Milliseconds $millisecs

The entry point to the script picks up the Windows PowerShell ISE font and font size and stores them in the $font and the $size variable. The reason for this, is because when I ran the script on my computer the first time, the last font to display was wingdings. That made changing to a more readable font a bit of a challenge – fun, but challenging. I decided it would be smarter to store the original configuration, and to set it back when the script is completed. This is seen here.

$font = $psISE.Options.FontName

$size = $psISE.Options.FontSize

 

Next the Get-Fonts function is called and I pass in values for the fontsize and for the millisecs parameters. Because these two parameters have default values specified in the function, it is not necessary to pass in new values unless you wish to modify the behavior of the function. When the function completes, the fontname and the font size are set back to the original values. This is seen here.

get-fonts -fontSize 14 -milliSecs 500

$psise.options.FontName = $font

$psISE.Options.FontSize = $size

When you run the script, the font name at the bottom of the output pane is the one being currently displayed. You will see the font sizes appear to change, but that is because some fonts are larger than others are; the actual font size that was used when the function is called is the size font being displayed. This is a good point to keep in mind and one reason I wrote the script.

Join us tomorrow as we begin a brand new week on the Script Center. We will talk about searching Active Directory Domain Services (AD DS) from Windows PowerShell. We would love you to follow us on Twitter or Facebook. If you have any questions, send email to us at scripter@microsoft.com or post them on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

 

Ed Wilson and Craig Liebendorfer, Scripting Guys

 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • -Unique on the select makes it unbearably slow.  Because the duplicates are all returned one after another it would be better to just do a quick check to see if the current font was the last font i.e.

    $lastfont = $psISE.Options.FontName

       [windows.media.fonts]::systemTypeFaces |Select-Object -Property fontFamily |ForEach-Object {

           if ($_.fontFamily.source -ne $psISE.Options.FontName) {

               $_.fontFamily.source

               $psISE.Options.FontName = $_.fontFamily.source

               $psISE.Options.FontSize = $fontSize

               Start-Sleep -Milliseconds $millisecs

           }

       }

    btw, I made the above by accident.  I intended on setting $lastfont in each iteration of the loop, but I missed it.  Somehow it still works.  Even though it doesn't say it's a ref it looks like $lastfont becomes a ref to $psISE.Options.Fontname.

  • Tome, You are correct, thanks for pointing that out. What a great addition. Thanks.