This is one of the first functions I wrote.  I wanted to mark my place in the console window or transcript, and rather than make a custom one, I thought I’d make a function and chuck it in a module, then I could use it in other scripts/modules to save a lot of repetition.  For some reason it all got out of hand, and I now wanted a console-width banner with a centered title, which needs to be calculated based on the console width.

This also comes in handy for indicating which account you are running under.  I put a Get-TitleBar “Running as $($env:username)” in my PS profile for each account, using yellow on black for standard.  red on black for admin, red on white for domain admin.

What I thought would be the annoying bit was lining up the edges and centering it.  Because our console uses a monospace font, it’s not difficult to work out how wide it’s going to be once you know what it’s going to say, so the maths was simple.  I also added in character choice for the title border. I posted it to a thread on Reddit and they suggested selecting colours, so in it went.

And here it is.  I pulled it out of my module, tidied the code up considerably and added some comments.  And removed a swear word.  Two swear words.  This was written in PowerShell2.0, so I imagine it will still work with it and anything newer.

Function Get-TitleBar {
Makes a pretty title bar
Do your scripts need some spice?
Do you find the top of your script output is hard to find in the logs?
Are people not immediately impressed with your script?
You need Get-TitleBar, the all-in-one output titling solution
Stick it in quotes, for the love of Neptune.
Can be any ASCII character, default is '-'
(Alias FG) Foreground Colour eg. White 
(Alias BG) BackGround Colour eg. Blue 
Get-TitleBar "Halp, yo"
Will write a title bar to the host
Get-TitleBar "Halp, yo" -fg white
Will write a title bar to the host with white text
Get-TitleBar "Halp, yo" -bc "^" -bg darkgreen
Will write a title bar to the host with carets for the borders and a dark green background
A few.
A title bar.
The colours are based on the console color scheme, so if you've changed them, it might get tricky.
param (
 [string]$Title = "No Title",
 [string]$BorderChar = "-",
 [string]$ForeColor = "Yellow",
 [string]$BackColor = "Black"

# Get those colours into a hashtable for splatting out later
$ColorSplat = @{ForegroundColor = $ForeColor; BackgroundColor = $BackColor}

# Get the length of the string and make the 'centre' bits of the border the same length
$border = $BorderChar * ($title.length +4)

# Set up the bookends, which are the blank areas beside the centered title
# This makes sure the background colour covers the width of the window
$left = " " * (($host.UI.RawUI.windowsize.width/2)-($title.length/2)-2)
$right = " " * ((($host.UI.RawUI.windowsize.width/2)-($title.length/2)-2)-1)

# Throw in the borders and title
Write-Host "$left$border$right" @ColorSplat
Write-Host "$left| $title |$right" @ColorSplat
Write-Host "$left$border$right" @ColorSplat
set-alias Title Get-TitleBar

Title "Witness me!"
     | Witness me! |

Leave a Reply

Your email address will not be published. Required fields are marked *