Skip to content
Raymond Piller edited this page Jan 13, 2023 · 5 revisions

PSWriteLog is a tool to standardize logging without the need to use new function calls in your PowerShell scripts. Just configure the log location (or don't) and start logging with your standard Write functions.

Quick Start

Before you do anything, install PSWriteLog. Create a new script, mine will be called foo.ps1 and will contain the following lines:

#Requires -Modules PSWriteLog
Write-Host 'Hello World!'

I love how clean and simple that is! However, the #Requires statement will terminate if you don't have PSWriteLog installed. Since we're not introducing any new functions, PSWriteLog shouldn't be required to just run the script. To ensure there are no errors if you share your script with someone that doesn't have PSWriteLog installed, you will want to use Import-Module.

Description

PSWriteLog has a private function called Write-Log that does all the logging for you. It's a private function because we don't want you to use it. Instead, we've created proxy functions for the Write-* functions so you don't have to learn to use a new function. These proxy functions keep the original functionality of the function intact, while also sending the outputted message to the Write-Log function. By default, Write-Log will write messages to a log file in CMTrace compatible format, but Legacy (plain-text) file format is also available. To configure PSWriteLog, what you need to do is change the default actions of the Write-Log parameters. You can specify default parameters using environment variables. Here's an example of specifying the Log file path and log type globally:

$env:PSWriteLogFilePath = "${env:SystemRoot}\Logs\MyApp.log"
$env:PSWriteLogType = 'Legacy'
Clone this wiki locally