Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Friday Fun: Aren’t You Special

Posted on January 30, 2015January 30, 2015

crayonsIf you've been following my work for any length of time you know I am constantly going on about "objects in the pipeline". But PowerShell is flexible enough that you should be able to use it as it meets your needs, provided you know the limitations for whatever path you take. Sometimes you really just want to read information on the screen and you want it to be as meaningful as possible. For today's fun let me show you how to add some special characters that might be just what you need to spice up an on screen display.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

First, you need to know what you can display and how. You will be using the [CHAR] class. Don't worry too much about details. You can use the [CHAR] class to display characters other than the usual alphanumeric and symbol characters on your keyboard. I also need to point out that everything I am showing you will only work in the PowerShell console, not the ISE. Here's how you can see some of these characters.

(1..6).foreach({[pscustomobject]@{char=$_;value=$_ -as [char]}}) | format-table -AutoSize

This snippet is the same as displaying [char]1 through [char]6.

You'll also find these ranges of interest.

(11..31).foreach({[pscustomobject]@{char=$_;value=$_ -as [char]}}) | Format-Table –AutoSize
(161..190).foreach({[pscustomobject]@{char=$_;value=$_ -as [char]}}) | Format-Table -AutoSize

Or use this one-liner to display them all.

((1..6)+(11..31)+(161..190)).foreach({[pscustomobject]@{char=$_;value=$_ -as [char]}}) | Format-Table -AutoSize

Now that you know what is possible, let's use some of these characters. How about a visual indicator if a service is running or not?

get-service | Sort Displayname |
Select DisplayName,
@{Name="Indicator";Expression={
Switch ($_.status) {
Running {[char]30}
Stopped {[char]31}
Default {[char]16}
}
}} | Format-Table -AutoSize -HideTableHeaders

I'm creating a custom property called Indicator which will display a special character based on the service status. To make it pretty to read I'm formatting as a table and hiding the header since I can tell from the output everything I need to know.

Remember I mentioned limitations? The special characters cannot be sent to a file, converted to HTML or exported. All you can do is run the code and read the screen. But that may be all you require. Well, technically you can direct the output to a file with Out-File. You won't see the characters in Notepad but you should when you use Get-Content.

Let's take the service example to another level. Using the Win32_Service class from WMI, we can tell if a service is set to Auto start but is not running. Let's use the same type of indicator for the service status and an indicator if an Auto start service isn't running.

#filter out disabled services
Get-CimInstance -ClassName Win32_Service -filter "Startmode <> 'disabled'" | 
Sort Displayname | 
Select @{Name="Displayname";Expression={
 if ($_.DisplayName.length -ge 50) {
  #truncate long displaynames
  "$($_.displayname.substring(0,50))..."
  }
  else {
    #use the full displayname
    $_.displayname
  }
  }},
@{Name="Indicator";Expression={
  Switch ($_.state) {
   Running {[char]30}
   Stopped {[char]31}
   Default {[char]16}
  }
}},
@{Name="Attention";Expression={
 if ($_.startmode -eq 'Auto' -AND $_.state -ne 'Running') {
   [char]15
 }
}} | Format-Table -AutoSize -HideTableHeaders

I used [CHAR]15 as my indicator. You know what? Since we know we can't do anything with the output other than read it on the screen (you are allowed to do this), let's go all out.

Get-CimInstance -ClassName Win32_Service -filter "Startmode <> 'disabled'" | 
Sort Displayname | 
Select @{Name="Displayname";Expression={
 if ($_.DisplayName.length -ge 50) {
  #truncate long displaynames
  "$($_.displayname.substring(0,50))..."
  }
  else {
    #use the full displayname
    $_.displayname
  }
  }},
@{Name="Indicator";Expression={
  Switch ($_.state) {
   Running {[char]30}
   Stopped {[char]31}
   Default {[char]16}
  }
}},
@{Name="Attention";Expression={
 if ($_.startmode -eq 'Auto' -AND $_.state -ne 'Running') {
   [char]15
 }

}} | foreach {

write-Host ($_.DisplayName).padright(55," ")  -NoNewline

switch -Exact ($_.indicator.toInt32((Get-Culture).NumberFormat)) {
 30 { $c = "green" }
 31 { $c = "red" }
 16 { $c = "yellow" }
 default { $c = "white"}
}

Write-Host $($_.indicator.tostring()).Padleft(3) -ForegroundColor $c -NoNewline

If ($_.attention) {
    write-host $($_.attention.tostring()).PadLeft(3) -ForegroundColor Yellow
}
else {
    Write-Host " "
}
}  #foreach

This code again uses WMI and the special characters, but then it looks at special character properties I created and determines a color to use for the output. The tricky part was converting the [CHAR] class back into an integer so that I could determine what color to use.

switch -Exact ($_.indicator.toInt32((Get-Culture).NumberFormat)) {
 30 { $c = "green" }
 31 { $c = "red" }
 16 { $c = "yellow" }
 default { $c = "white"}
}

There are probably other ways to achieve the same result, but this works for me.

So have some fun with these special characters. Realize there are limitations to what you can do with the output, but there are probably no limitations on how you might use them. If you come up with something interesting I hope you'll share.

Let's have fun out there.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d