Thursday, June 23, 2011

Learning Powershell

I'm studying Powershell and have written a couple of small but useful scripts in it. Just for fun I decided to try my hand at something a little different when I saw an article in the TechNet Library about how to list Fonts on a computer. This script is adapted from that one and is actually kinda useful. It displays ONLY the TrueType Fonts on a PC, as did the original. However, where the original only displayed the Font Name, this one also displays a little more text and also the name of the logged on user. Also, the original script, if re-run, appended the new list onto the old one (appended the content of $objFonts to $strHTML); I am clearing $strHTML to that only fresh results appear.

Clear-Variable strHTML
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$UserName = (Get-Item 'HKLM:\Software\Microsoft\Windows NT\CurrentVersion\Winlogon').GetValue('DefaultUserName')
$objFonts = New-Object System.Drawing.Text.InstalledFontCollection
$colFonts = $objFonts.Families
$objIE = New-Object -com "InternetExplorer.Application"
$objIE.Navigate("about:blank")
$objIE.ToolBar = 0
$objIE.StatusBar = 0
$objIE.Visible = $True
$objDoc = $objIE.Document.DocumentElement.LastChild
foreach ($objFont in $colFonts)
{
$strHTML = $strHTML + "<font size='5' face='" + $objFont.Name + "'>" + 'This ' + $objFont.Name + ' font is on the computer of '+ $UserName + "</font><br>"
}
$objDoc.InnerHTML = $strHTML

No comments:

Post a Comment