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

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: Export-CliXml ConvertTo-JSON 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: (Hashdata.Object.ps1) 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 t

Toying with audio in powershell

Controlling mute/unmute and the volume on you computer with powershell. Add-Type -TypeDefinition @' using System.Runtime.InteropServices; [Guid("5CDF2C82-841E-4546-9722-0CF74078229A"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] interface IAudioEndpointVolume { // f(), g(), ... are unused COM method slots. Define these if you care int f(); int g(); int h(); int i(); int SetMasterVolumeLevelScalar(float fLevel, System.Guid pguidEventContext); int j(); int GetMasterVolumeLevelScalar(out float pfLevel); int k(); int l(); int m(); int n(); int SetMute([MarshalAs(UnmanagedType.Bool)] bool bMute, System.Guid pguidEventContext); int GetMute(out bool pbMute); } [Guid("D666063F-1587-4E43-81F1-B948E807363F"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] interface IMMDevice { int Activate(ref System.Guid id, int clsCtx, int activationParams, out IAudioEndpointVolume aev); } [Guid("A95664D2-9614-4F35-A746-DE8DB63617E6"), Inte

Creating Menus in Powershell

I have created another Powershell module. This time it is about Console Menus you can use to ease the usage for members of your oranization. It is available on GitHub and published to the PowershellGallery . It is called cliMenu. Puppies This is a Controller module. It uses Write-Host to create a Menu in the console. Some of you may recall that using Write-Host is bad practice. Controller scripts and modules are the exception to this rule. In addition with WMF5 Write-Host writes to the Information stream in Powershell, so it really does not matter anymore. Design goal I have seen to many crappy menus that is a mixture of controller script and business logic. It is in essence a wild west out there, hence my ultimate goal is to create something that makes it as easy as possible to create a menu and change the way it looks. Make it easy to build Menus and change them Make it as "declarative" as possible Menus The module supports multiple Men