Worth Corner, Crawley, RH10 7SL

Support Hours - Mon-Sat, 8.00-18.00

Welcome to our SQL Server blog

This blog was created in order to pass on some of our knowledge and advice in the hope that the community might find it useful.

Obviously Microsoft SQL Server is our area of expertise and therefore this is our most thorough blog with everything from the basic through to the the incredibly advanced and, as such, we should have something for everyone within our pages.

Writing to the Console
Published: Jan 17, 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.

SQL  World  CEO
Kevin  Urquhart

iPhone Selfie

I am a SQL Server DBA, Architect, Developer, Trainer, and CEO of SQL World. This is my blog in which I’m simply trying to share my SQL knowledge and experiences with the world.


© Copyright 2020 SQLTraining Ltd.