Writing to the Console
Published: Jun 29, 2022
When you start testing and writing code in PowerShell, one of the most useful things you can do is to return data to the console in order to see what’s happening, what you’ve done etc and keep track of your code as it runs.
PowerShell provides this in a very simple way and, unlike ECHO in DOS, PowerShell provides us with some formatting too, which can make your code look nice and friendly to both testers and developers.

So, without further ado, let’s get straight to it…

First thing to note is that I’m going to be using the PowerShell ISE. This was explained in my previous post and it’ll be what I use throughout as I’m not a fan of the console as I only use PowerShell in the scripting format rather than for odds and ends as per DOS:

So, let’s get to it:

Write-Host("This will be written to the console")

If you execute the above on your own machine (press the green Play button or hit F5) you will see the below:

Now, this looks confusing to me… the problem being that the Console is showing what we passed in as well as the output… so we need a way to make it simpler. The way we do this is to clear the console before we run anything. By doing this we only show the output of our code and not the code itself:

("This will be written to the console")

This only needs to be placed once at the top of the script in order to work (I’m used to this in the same way that I would habitually place SET NOCOUNT ON at the top of my SQL scripts):

The only other piece of formatting I like to force is a line break between the results and the next PS Input Line. The only way I know how to do this is with a blank line:

Write-Host("This will be written to the console")

NOTE – All of this is formatting that I like to see… if you aren’t bothered then please ignore. Also, there may be better ways of doing these things, but this will get you started with simple things that just “work” and that’s the key.

One new part of PowerShell is the ability to make things look slightly less “console-like” by adding some colour:

("This is red") -ForegroundColor Red
Write-Host("This is yellow") -ForegroundColor Yellow
Write-Host("Background Gray") -BackgroundColor Gray
Write-Host("Background Black") -BackgroundColor Black
Write-Host("Red on White") -BackgroundColor White -ForegroundColor Red

This may look stupid and pointless, but I use it a lot for formatting within logged output of a script.

Here’s an example from my SQL Server Health Check scripts (which are all PowerShell based):

Hence you can see that this makes for a much easier to read and digest output than a generic console.

Leave a Comment
Your email address will not be published. All fields are mandatory.
NB: Comments will only appear once they have been moderated.

Kutech  CEO
Kevin  Urquhart


I am a SQL Server DBA, Architect, Developer, Trainer, and the owner and CEO of Kutech. This blog has been going for nearly 10 years now over several guises and this is its new home. I hope you find it useful whatever your endeavour.


© Copyright 2021 Kutech Solutions Ltd.

Our site uses cookies only to personalise content and analyse traffic. We do not use cookies for advertising. Policy