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: Crossing the Border with PowerShell

Posted on March 24, 2017

Today's Friday Fun post, as most of these are, is a little silly and a little educational. Because I obviously am avoiding getting any real work accomplished, I worked on a little project that would add a border around a string of text. I often write formatted text to the screen to display information and thought it would be nice to be able to add sometime of border.

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!

My goal was to take a string like "Friday Fun" and write it to the console centered in a bordered box - something like this:

**************
* Friday Fun *
**************

I had to get the length of the string and add 2 to it to allow for a space on either side. The top and bottom lines were as simple as writing the character times the length.

"*"*20

The middle part was simply a matter of concatenating the character and the text.

$body = "$Character $text $Character"

I then used a here string to create the final result.

#define a here string with the final result
$out = @"
$tabs$line
$tabs$body
$tabs$line
"@

And yes, I included a feature where the result could be tabbed or indented a certain number of times. You can find the complete function as a Github gist.

https://gist.github.com/jdhitsolutions/0bbd6b64c107d7da23e65359c4d0e25c

Here are some examples of the function in action.

image

You can border any single line of text. For example, I have a short function to get some basic system information.

Function Get-Status {
    [cmdletbinding()]
    Param(
        [string]$Computername = $env:computername,
        [switch]$AsString
        )
    $OS = Get-Ciminstance -ClassName Win32_OperatingSystem -ComputerName $computername
    $uptime = (Get-Date) - $OS.lastBootUpTime
    $pctFreeMem = ($os.FreePhysicalMemory / $os.TotalVisibleMemorySize)*100
    $CDrive = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "deviceid='c:'" -ComputerName $computername
    $pctFreeC = ($CDrive.FreeSpace/$CDrive.size)*100
    $status = [PSCustomObject]@{
        Computername = $OS.pscomputername
        Uptime = $uptime
        PctFreeC = [math]::Round($pctFreeC,2)
        PctFreeMem = [math]::Round($pctFreeMem,2)
    }
    if ($AsString) {
        "{0} Uptime:{1} %FreeC:{2} %FreeMem:{3}" -f $status.computername,$status.uptime,$status.PctFreeC,$status.PctFreeMem 
    }
    else {
        $status
    }
}

I included a parameter to write the result as a formatted string. I can now put a line like this in my PowerShell profile script:

. C:\scripts\GetStat.ps1
. C:\scripts\Add-Border.ps1

cls
write-host "`n"
get-status -AsString | add-border | write-host -ForegroundColor Green
write-host "Time to get to work, Jeff." -ForegroundColor yellow
write-host "`n"
cd c:\

image

Perhaps you can think of other fun ways to add borders to your script or command output. If so, I'd love to hear about it. Enjoy!


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

2 thoughts on “Friday Fun: Crossing the Border with PowerShell”

  1. Mat says:
    March 28, 2017 at 6:35 am

    Hello Jeffrey
    Nice script, i think i’ll use it !
    But, when you do both InsertBlanks and Tab, only the first line of the Body here-strings si tabbed, not the rest.

    Here is how i deal with it :

    Process {
    Write-Verbose “[$((Get-Date).TimeofDay) PROCESS] Processing ‘$text'”
    #get length of text
    $len = $text.Length

    Write-Verbose “[$((Get-Date).TimeofDay) PROCESS] Using a length of $len”
    #define a horizontal line
    $line = $Character * ($len+4)

    $tabs = “`t”*$tab

    if ($insertBlanks) {
    Write-Verbose “[$((Get-Date).TimeofDay) PROCESS] Inserting blank lines”

    $body = @”
    $tabs$character $((” “)*$len) $character
    $tabs$Character $text $Character
    $tabs$character $((” “)*$len) $character
    “@
    }
    else {
    $body = “$tabs$Character $text $Character”
    }

    $out = @”
    $tabs$line
    $body
    $tabs$line
    “@
    #write the result to the pipeline
    $out
    } #process

    1. Jeffery Hicks says:
      March 28, 2017 at 1:11 pm

      Yeah, that’s an annoying bug. Your workaround helped. I’ve updated the gist.

Comments are closed.

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