#PowershellBasics: Writing to the screen

8

May 20, 2021 by Kenneth Fisher

Writing to the screen is a really basic debugging technique. That said, since I’m really new with Powershell this is something I needed to get good at quickly. There are two commands that I’m making use of. The first is Write-Host. This is similar to Print in SQL Server. Print is pretty basic and your only option is to pass in what you want printed, and it’s going to go on it’s own line. Write-Host on the other hand has a number of additional flags that you can use. A few of my favorites are -nonewline, -foregroundcolor, and -backgroundcolor.

By default, Write-Host, much like Print, writes it’s output and then adds a new line afterwards. -nonewline let’s you bypass that.

$var = 4
Write-Host "Var is: " -nonewline
Write-Host $Var
This is great but can be kind of hard to see when you’ve tested something several times, and it prints out dozens of lines of code and/or debugging information. So the next thing I do is use the Clear-Host command, which can also be abbreviated as CLS. If you are old enough you’ll remember CLS from the DOS days where it stood for clear screen. I’ve gotten into the habit of adding it to the top of my code so that it clears the host (the output screen) before my code runs. I’m not going to bother to demonstrate because it’s really pretty basic.

Last but by no means least –foregroundcolor, and -backgroundcolor. If I’m generating a lot of output and throwing a debug in the middle, it can take a bit to find the information I’m looking for. By changing the background and/or foreground (the text itself) colors I can make it far easier to see. If you were wondering why I split the write for the description and the variable itself, it’s so I could do two different colors for the text. There might still be a way to do this in a single line but I don’t know it and I find this fairly easy to follow so I’ll stick with it for now.

Clear-Host
$var = 4
Write-Host "Var is: " -nonewline -foregroundcolor black -backgroundcolor red
Write-Host $Var -BackgroundColor red

8 thoughts on “#PowershellBasics: Writing to the screen

  1. Michael says:

    Another option is…

    Write-Debug
    Write-Verbose

    Which can be turned on or off with a preference variable. So when building your script you can have output to the screen and once you are done and ready for production you can turn off the output with one change to the preference variable.

  2. Chad Estes says:

    I second Michael’s suggestion about using Write-Debug instead of Write-Host, especially if you plan on adding your code to a SQL Agent job where output is strictly verboten.
    It’s much easier to go from troubleshooting your code in an ISE window by changing one variable, copy the code, and paste it into the SQL Agent job, than trying to remember to comment out all of the write-host statements you used while testing your code before transitioning to the SQL Agent job. Especially when you’ve got in excess of 20 write-host statements; not sure about y’all, but inevitably I always miss one.

  3. Terrance says:

    You can write it in a single line by using a semicolon after the first statement then continuing with the next statement like Write-Host “Var is ” -NoNewLine -ForegroundColor black -BackgroundColor red; Write-Host $var -BackgroundColor red

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 3,570 other followers

Follow me on Twitter

ToadWorld Pro of the Month November 2013
%d bloggers like this: