Microsoft has released a new version of PowerShell 7.2. After installation of latest Microsoft PowerShell I was reading release notes and what's new document on https://docs.microsoft.com/en-us/powershell/scripting/whats-new/what-s-new-in-powershell-72?view=powershell-7.2. While reading, there is a new experimental feature introduced - ANSI FileInfo color support. Under this feature there is a new variable named $PSStyle is introduced. Which help you to rendering of output data on the Powershell console. Before this feature I was using hard way - escape sequences which example has shown in the last of this article. But I must say with new $PSStyle It has became very easier now.
Below are the $PSStyle variable properties.
To use $PSStyle to format the output you can use the above properties and format the output on the screen.
Write-Output "$($PSStyle.Background.BrightRed)HAPPY $($PSStyle.Underline)$($PSStyle.Bold)NEW YEAR$($PSStyle.Reset)"
Out put of the above will be HAPPY NEW YEAR. You can use bold, italics, underlined and many more formatting in the output on the single line, You can add decoration and animate the text. below is the full fledged script I tried to be little creative.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
function HappyNewYear { Clear-Host #Created by http://vcloud-lab.com $windowSize = $host.UI.RawUI.WindowSize $line1 = "$([char]9608) $([char]9608) $([char]9608)$([char]9608)$([char]9608) $([char]9608)$([char]9608)$([char]9608) $([char]9608)$([char]9608)$([char]9608) $([char]9608) $([char]9608) $([char]9608)$([char]9608) $([char]9608) $([char]9608)$([char]9608)$([char]9608) $([char]9608) $([char]9608) $([char]9608) $([char]9608) $([char]9608)$([char]9608)$([char]9608) $([char]9608)$([char]9608)$([char]9608) $([char]9608)$([char]9608)$([char]9608)" $line2 = "$([char]9608)$([char]9608)$([char]9608) $([char]9608)$([char]9604)$([char]9608) $([char]9608)$([char]9604)$([char]9608) $([char]9608)$([char]9604)$([char]9608) $([char]9608)$([char]9604)$([char]9608) $([char]9608) $([char]9608) $([char]9608) $([char]9608)$([char]9604) $([char]9608) $([char]9608) $([char]9608) $([char]9608)$([char]9604)$([char]9608) $([char]9608)$([char]9604) $([char]9608)$([char]9604)$([char]9608) $([char]9608)$([char]9604)$([char]9600)" $line3 = "$([char]9608) $([char]9608) $([char]9608) $([char]9608) $([char]9608) $([char]9608) $([char]9608) $([char]9608) $([char]9608)$([char]9608) $([char]9608)$([char]9604)$([char]9604) $([char]9608)$([char]9608)$([char]9608)$([char]9608)$([char]9608) $([char]9608) $([char]9608)$([char]9604)$([char]9604) $([char]9608) $([char]9608) $([char]9608) $([char]9608)" $hSpaces = [System.Math]::Round(($windowSize.Width - $line1.length) / 2) $vSpaces = [System.Math]::Round(($windowSize.Height - 3) / 2) "`n" * $vSpaces "$(' ' * $hSpaces)$($PSStyle.Blink)$($PSStyle.Bold)$($PSStyle.reverse)$($PSStyle.Background.Brightgreen)$line1$($PSStyle.Reset)" "$(' ' * $hSpaces)$($PSStyle.Blink)$($PSStyle.Bold)$($PSStyle.reverse)$($PSStyle.Background.Brightgreen)$line2$($PSStyle.Reset)" "$(' ' * $hSpaces)$($PSStyle.Blink)$($PSStyle.Bold)$($PSStyle.reverse)$($PSStyle.Background.Brightgreen)$line3$($PSStyle.Reset)" "`n" * $($vSpaces / 2) "$($PSStyle.Background.BrightRed)HAPPY $($PSStyle.Underline)$($PSStyle.Bold)NEW YEAR$($PSStyle.Reset)" "`n" * $(($vSpaces / 2) - 2) } HappyNewYear |
This is the output of script on the screen. You can download HappyNewYear.ps1 script here or it is also available on github.com.
Read more details about $PSStyle on: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_ansi_terminals?view=powershell-7.2. While using this feature you can also use more RGB colors using below format.
$PSStyle.Background.FromRgb(145, 235, 120)
$PSStyle.Background.FromRgb(0xf6f5dc)
Before using new feature I was using literal ANSI escape sequence, more about it you read it on https://en.wikipedia.org/wiki/ANSI_escape_code. Just run below commands on Powershell and above see the magic output.
Write-Output "`e[5;37mHappy New Year`e[0m" Write-Output "`e[1m`e[5;37mHappy New Year`e[0m"
Useful Articles
Powershell Trick: Convert text to ASCII art 1
Generate random password GUI using powershell
Part 1: Create WPF XAML powershell GUI form with Visual studio
Part 2: Powershell and WPF: Build GUI applications tutorial
Part 3: Create shorter Microsoft Powershell WPF automated clean script
Powershell WPF Themes guide step by step
Part 2: Powershell WPF Themes guide step by step
Part 3: Powershell wpf MahApps.Metro theme step by step
Powershell WPF MahApps.Metro update theme library
Powershell WPF custom Image based ProgressBar
Powershell WPF Charts dashboard demo