How To Log Scripts In PowerShell

By: Mason Prewett

SharePoint & Business Intelligence Consultant @ Key2 Consulting

 

Useful Logging In PowerShell

PowerShell is a powerful language to use when completing repetitive tasks on a system. I usually find myself needing to use PowerShell when I have the following common requirements:

  1. Completing large tasks that take a long time to process
  2. Needing logic to be processed on a schedule
  3. The processing of dynamic sets of data

All of these requirements make it important to analyze exactly what the script did after it’s complete. This is where logging in your scripts comes in handy.

*Note – this article will only focus on logging to a text file.

Let’s Begin

Open Windows PowerShell ISE. Search the start menu if you have never opened this before. It’s always a good idea to right click it and “Run as administrator” as well.

Let’s start off with the most basic log to file:

Running this code will log the message “This is my first log to file” to a text file located at C:\PSLog.txt. The “>>” operator writes a single line to a text file. There is no need to create, open, or close the file when writing to it because PowerShell takes care of this for you.

Run this code for a second time and you’ll notice it will append the same message on the next line of the text file.

I usually want a fresh log file for each code execution. Use the following code to delete the file if it exists and write to a brand new file.

Great! This is logging to a text file and ensuring that each execution overwrites the last.

I also like to add the date and time of every log each time one is written. This helps me understand how long each step is taking. Having to do this in the code above would be a little messy and not something I would want to have to do over and over. Let’s also look at making this a little more modular by using a function.

C:\PSLog.txt

6/30/2017 10:42:18 AM – Hello from a function call

6/30/2017 10:42:18 AM – This is easy to use

6/30/2017 10:42:18 AM – A lot cleaner too

This code allows us to simply call the function by name and add the log message as a parameter in a familiar PowerShell format: LogMessage -Message “Hello World”. Now the log file has the date and time automatically added before each message. This is because all we have to do is pass the message to our function and it handles adding the time and date each time using the (Get-Date).ToString code. This is looking a lot better for use in a PowerShell script that has to perform a lot of logging.

*Note – Make sure to add the function definition before calls are made to it. This may appear to work if you run it more than once in ISE, but that’s because it stores it in memory. If you schedule a script to run, the function will have to be defined before it is called or you will not get any logging.

Let’s take it a step further. This is a common operation that I could use in all of my scripts, so let’s make it even more modular using a separate logging script.

C:\Logger.ps1

C:\LogExample.ps1

Now we have a logging script that can be reused in any future scripts that are written. The “dot notation” (. “C:\Logger.ps1) includes the content of an external script in a local script. It essentially pastes the content of the script where it is used. To be clear, that is “dot space ‘file name’”. Additionally, I added another script locally that calls the function and provides the LogFile so I don’t have to do this every time.

Leave a Reply

Your email address will not be published.