#PowershellBasics: Writing to the screen
8May 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

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

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.
Very nice! I’ll have to remember that one!
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.
Yep. I do love my colorful with my write-host output to the screen though. lol
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
Interesting. For that type of thing I prefer to keep it on separate lines just for formatting (although that’s just a personal preference) the semicolon thing is really handy.
Yeah, its not the best way but wanted to point out that it can be done.
No, I appreciate it. Sometimes that’s really important