Preference variables not required (see below).
The Write-Section
function is a custom PowerShell function designed to enhance the readability of console output. It prints text messages surrounded by a section divider made up of hash #
characters. This effectively gives a border to your PowerShell output.
- Border: Provides a decorative border around your text for better visibility.
- Stand Out: Easy way to separate messages that you want to stand out.
- Customizable Message Types: Supports various message types including:
- Warnings
- Debug
- Information
- Verbose
- Output
- Graceful Word Wrapping: Automatically wraps messages to fit within the console width without cutting off any characters.
- Fully Enclosed Text: Pads messages so that the
#
character appears before and after the messages lines - Readability: Enhances the readability of your PowerShell console output.
Parameter | Required | Description |
---|---|---|
Message |
Yes, default parameter if not specified explicitly | The message to be printed within the section divider. |
Type |
No | The type of message to display; defaults to Output . |
Type Value | Description |
---|---|
Warning |
Displays the message using Write-Warning . |
Debug |
Displays the message using Write-Debug . |
Information |
Displays the message using Write-Information . |
Verbose |
Displays the message using Write-Verbose . |
Output |
Displays the message using Write-Output (default). |
Unfortunately Write-Error
is not supported in this function because it outputs a stack trace and would cause issues with the formatting.
If you want to test with Debug/Verbose/Warning, you'll have to set these variables to Continue
. Otherwise you'll have to use the -Debug
, -Verbose
, etc. arguments when launching a script that supports CmdletBinding()
. More about preference variables in PowerShell.
$VerbosePreference = 'Continue'
$DebugPreference = 'Continue'
$InformationPreference = 'Continue'
For basic usage you do not need the above variable assignments.
# Basic usage
Write-Section -Message "This is a basic message."
# Using a warning type
Write-Section -Message "This is a warning message." -Type "Warning"
# Using an information type
Write-Section -Message "This is an information message." -Type "Information"
# Using a debug type
Write-Section -Message "This is a debug message." -Type "Debug"
# Using a verbose type
Write-Section -Message "This is a verbose message." -Type "Verbose"
# Implicit use of the Message parameter
Write-Section "Implicit message parameter example. You can provide the message without explicitly naming the Message parameter, making the function even more convenient to use."
# Additional Implicit use of the Message parameter and Type of Debug
Write-Section "Additional implicit message parameter example. You can provide the message without explicitly naming the Message parameter, making the function even more convenient to use, while still specifying the type." -Type "Debug"
######################################################################################################################
# This is a basic message. #
######################################################################################################################
WARNING: #############################################################################################################
WARNING: # This is a warning message. #
WARNING: #############################################################################################################
######################################################################################################################
# This is an information message. #
######################################################################################################################
DEBUG: ###############################################################################################################
DEBUG: # This is a debug message. #
DEBUG: ###############################################################################################################
VERBOSE: #############################################################################################################
VERBOSE: # This is a verbose message. #
VERBOSE: #############################################################################################################
######################################################################################################################
# Implicit message parameter example. You can provide the message without explicitly naming the Message parameter, #
# making the function even more convenient to use. #
######################################################################################################################
DEBUG: ###############################################################################################################
DEBUG: # Additional implicit message parameter example. You can provide the message without explicitly naming the #
DEBUG: # Message parameter, making the function even more convenient to use, while still specifying the type. #
DEBUG: ###############################################################################################################