Skip to main content

Powershell – Log like you mean it

How do you do logging in powershell? Why should you do logging? What should you log? Where do you put your log? How do you remove your log? How do you search your log? All important questions and how you answer then depends upon what your background is like and the preferences you have. This will be a 2 part blog post and this is part 1.

Why should you log?

Well it is not mandatory, however I have 2 reasons:

  1. Help with debugging a script/module/function
  2. Self documenting script/module/function

Firstly; Do you know any program that does not contain any bugs? Working with IT for the last 2 decades, I cannot name one. When you create scripts/modules/functions, you will create bugs, that is where they live and try to make your life a living mess.

Secondly: Adding a little extra information to your logging will make them self documenting. Do you like writing documentation? Well I normally am not fond of it and use logging while debugging to get two birds with one stone.

What should you log?

Anything you like is the short and sweet answer. When troubleshooting something less is less and more is more. Detailed logging is indispensable when you are looking for a clue or reason for an error or wrong output, especially if you did not write the darn thing. For me it is also a question of context. Small scripts normally kind of limits it self from the “rest”. Nested functions, a script file with a handful of functions or a full-blown module require maybe a little more from your logging design.

What information you choose to log, is entirely up to you. Bear in mind that the information should give useful input to any person troubleshooting your work. Sometimes it is difficult, however it as a rule of thumb I usually do excessive logging.

In powershell there is a rule of conduct that states; “ You should not force your logging on your users” or something to that effect. Meaning the user of your creation should have the option to turn off your logging if it is not required. This of course relates to the console and not logging to files or other providers you may have available.

Where do you put your log?

Where ever you want. You may have a look if there is any corporate rule or procedure for where logs should be stored for future reference. There may also be rules regarding to for how long you should keep you logs or when a log should be shipped to an archive. If your corporate policy does not exist, you should be a good boy/girl and put some thought into this and perhaps suggest some guidelines to the paper pushers just for reference.

Use anything or what you are comfortable with. Just consider portability and where your script/module/function will live in the future. Will you always have access to a special database?

How do you search your log?

Well in what format is your log? If you use pure text and save it to a file, you have to search the file for some text that should give you a clue. Kind of obvious, however you get what you give.

If you want to treat your logging as objects in powershell, you have to create them first, store them for safe keeping and serialize them back to objects when you retrieve them, otherwise they will just be pure flat text files. Well unless you use ConvertFrom-String in WMF 5 which has a template approach to parsing pure text files.

How I do logging

Usually I create a function called Write-Verbose since I need to provide verbose logging to the script/function any way. When we do this, we use a little Powershell trick that overrides the cmdlet Write-Verbose. When Powershell tries to resolve your command, it will always first try to find a function with that name and invoke it. Here is an example:

The last line is the call to the "real" Write-Verbose cmdlet prefixed with it's namespace Microsoft.Powershell.Utility. This way, we can now write functions/scripts like this:




  1. Good Stuff. I used a similar approach but with full proxy functions (accepts every write-* option) and nlog. I turned it into a module you can grab here if you wanted to take it for a spin.

  2. Hi, glad you liked it. I will take a look at it, thank you!


Post a Comment

Popular posts from this blog

Developing PowerShell modules for REST APIs – Part1

Over the years I have developed different PowerShell modules for different web APIs. I thought it would be a good idea to write a 2 series post about how you could go about to do this. This will be a 2 part blog series where we will run through the entire process of building a module for a REST API. I will try my best to keep this as simple as possible and leave more advanced stuff for a follow up post if the interest is there.What you needDepending on your experience with source control and PowerShell in general, you might want to use GIT or some other software repro for the code. In addition we are going to create a test REST API using the splendid UniversalDashboard PowerShell module created by Adam Driscoll. It is available on the PowershellGallery. Other prerequisites are built-in to Powershell. I will assume that you will be following along using at least PowerShell version 5 or greater.
What is HTTP metods for REST API.The primary or most common HTTP verbs used are POST, GET, PU…

Serialize data with PowerShell

Currently I am working on a big new module. In this module, I need to persist data to disk and reprocess them at some point even if the module/PowerShell session was closed. I needed to serialize objects and save them to disk. It needed to be very efficient to be able to support a high volume of objects. Hence I decided to turn this serializer into a module called HashData.

Other Serializing methods

In PowerShell we have several possibilities to serialize objects. There are two cmdlets you can use which are built in:
Both are excellent options if you do not care about the size of the file. In my case I needed something lean and mean in terms of the size on disk for the serialized object. Lets do some tests to compare the different types:


You might be curious why I do not use the Export-CliXML cmdlet and just use the [System.Management.Automation.PSSerializer]::Serialize static method. The static method will generate the same xml, however we …

Build your local powershell module repository - ProGet

So Windows Powershell Blog released a blog a couple of days ago (link). Not too long after, a discussion emerged about it being to complicated to setup. Even though the required software is open source (nugetgalleryserver), it looks like you need to have Visual Studio Installed to compile it. I looked into doing it without visual stuidio, however I have been unable to come up with a solution. I even tweeted about it since I am not an developer. Maybe someone how is familiar with “msbuild” could do a post on how to do it without VS.

Anyhow one of my twitter-friends (@sstranger) came to the rescue and pointed me in the direction of ProGet, hence the title of this post. ProGet comes in 2 different licensing modes
Free (reduced functionality)Enterprise (paid version with extra features)The good news is that the free version supports hosting a local PowershellGet repository which was my intention anyway. So off we go and create a Configration that can install ProGet for us. This is the conf…