I always try to save time and keep consistency as much as I can on my daily tasks. I was thinking about that when writing a script to one of my customers and I had to deal with outputs as part of the process, so I thought that would be a good time to create a consistent function. The first visual language that I learned when I was a teenager was Visual Basic 3.0 and, of course, the coolest function that I fell in love was the msgbox, which created all those cool message boxes for me in a single line. Since I won’t be able to deliver the same graphical user experience in PowerShell that I had back in the day with Visual Basic, my decision was to use a similar interface from Linux (to remind me of the good old times with Red Hat).
The idea behind this PowerShell function below is to provide a simpler but similar experience. You just need to provide the caption, message, type (0 is good, 1 is warning and 2 is an error). I’m asking $MaxSize. Basically, I want to know how large your message throughout your script will be. If you know, then I can provide all the status aligned in the output by adding spaces between the message and the status icon. If I don’t know the value, I’m guessing 66 (big fan of “The Prisoner”!)
Keep in mind that we can use variables when passing parameters to the function. In the first example below, it will save some time to write into the code.
The PowerShell function can be found at my GitHub here.
Featured image: Shutterstock / TechGenix photo illustration
More PowerShell Basics articles
- Creating Hyper-V VMs from a CSV file with PowerShell
- PowerShell Quick Tip: Consistent output in Azure Automation Accounts
- PowerShell parameters: Controlling the input of your scripts
- Color-coding PowerShell operating system health reports
- WMI or CIM in your PowerShell scripts: Which should you use?