PowerShell WriteTo-Log Function

*** UPDATED! ***

I’ve discovered that if I used this function as I’d designed it originally, I couldn’t use it in a script that ran as a scheduled task since there’s no History in the $MyInvocation variable to draw from to name the log file.  So I re-wrote the function to accomodate this in the following way:

  1. If NO -Logfile parameter is passed to the function within a script, it will automatically name the resulting log file as originally described below
  2. If the -Logfile parameter IS passed to the function within a script, it will use the string value passed to it as the log file name to write to.
  3. If a $Script:Logfile variable is defined in the body of the script, and the -Logfile parameter is NOT passed in the script, the function will always use that string value defined.

This allows me to write a script and configure it to run as a scheduled task.  Then I just define a $Script:Logfile variable once at the beginning of the script like this:

$Script:Logfile = ‘.\’+(Get-Date).ToString(‘yyyyMMdd-HHmm’)+”-MyLogFileName.log”

This insures my script always uses the same log file name while it’s running and doesn’t have to rely on HistoryID.

I write a LOT of PowerShell scripts for my clients on almost every engagement I’m on.  It’s not unusual for one of those scripts to execute a command or lots of commands against a lot of user accounts.

In order to ensure I can keep track of what changes are being made to which users, and when the change was made, it’s helpful to have a log file for each execution of a script.

That’s why I wrote my own PowerShell function called WriteTo-Log.  This function creates a log file named with the date & time of the script execution, then the file name of the script (minus the extension) and adds a .log extension and places that log file in the same directory from which the script was executed. (note – not the same directory the script resides, but the directory from which the script was executed)

Example file name: .\2016-01-12-03.22-test-script.log

If you prefer the script use a different file name, you can use the -FileName parameter and pass your own file name to the script each time you invoke the function.

Each time the function is called, a new line is added to the log file with a date/time stamp and whatever string you pass to the function.  You can also add Foreground and Background color formatting and use the optional -OutputToScreen switch parameter to also echo the line out to the screen for easy review.

Example log entry: 2016-01-12 03.22.32 : Found a file!

The function can be easily copied/pasted at the beginning of every script I write.  Then whenever the script takes an action on a user account, I can execute my WriteTo-Log function and pass to it the details of the user and any success/fail results.

I’ve found that running this function in the ISE can cause problems because the ISE doesn’t like or support the Foreground & Background parameters the way I have implemented them.  For best results, save your script to a .ps1 and run it in a separate PowerShell window.

You can find the script an an example use below.  You can also find the script in the TechNet Gallery

Sample usage:

$processes = Get-Process
WriteTo-Log ("Found "+$processes.Count+" processes running") -OutputToScreen -ForegroundColor Green
foreach ($process in $processes) {
WriteTo-Log $process.processname -OutputToScreen -ForegroundColor Yellow

You can get the script here.