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

The core function is a hidden Write-Log function that is called by all the proxy functions above. Since it's a hidden function, you need to use environment variables to customize functionality. Here's an example of specifying the Log file path and log type globally:

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

ContinueOnError

  • Type: [switch]

If there's an error writing to the log file, a terminating error will be thrown. Unless of course, you've specified this switch.

$env:PSWriteLogContinueOnError = $true

DisableLogging

  • Type: [switch]

If there's a reason to toggle-off all logging for a bit, this is your mechanism.

$env:PSWriteLogDisableLogging = $true
Get-AllSuperSecretStuff -Verbose
$env:PSWriteLogDisableLogging = $null

FilePath

  • Type: [IO.FileInfo]
  • Default: Something like: %TEMP%\PowerShell Desktop 5.1.19041.1682 Internal.log

The default path is created with this command:

[IO.Path]::Combine($env:Temp, ('PowerShell {0} {1} {2}.log' -f @(
    $PSVersionTable.PSEdition
    $PSVersionTable.PSVersion
    $MyInvocation.CommandOrigin
)))

ℹ: The default file name will vary depending on your environment. Running the above command may yield slightly inaccurate results because $MyInvocation.CommandOrigin could vary between a few things, such as Internal or Runspace. If you want to know exactly where your log file is, configure it ...

Change the location by providing the full path to the log file.

$env:PSWriteLogFilePath = "${env:SystemRoot}\Logs\MyApp.log"

IncludeInvocationHeader

  • Type: [switch]

If you're familiar with Start-Transcript -IncludeInvocationHeader then you already know what this does. It logs some environment information before the next Write-Log call.

$env:PSWriteLogIncludeInvocationHeader = $true

We set $env:PSWriteLogIncludedInvocationHeader to True when it's written to ensure it's only done once per session. If you want it done again, just clear out that environment variable:

$env:PSWriteLogIncludedInvocationHeader = $null

LogType

  • Type: [string]
  • Default: CMTrace
  • Options: CMTrace, Legacy

Define the format for log messages. Will write messages to a log file in CMTrace compatible format, but Legacy (plain-text) file format is also available.

$env:PSWriteLogType = 'Legacy'

MaxLogFileSizeMB

  • Type: [decimal]
  • Default: 10.0

Log rotations are built-in. Archived logs are renamed from a .log extension to a .lo_ extension. If a .lo_ already exists, it'll be deleted.

$env:PSWriteLogMaxLogFileSizeMB = 3.14

Clone this wiki locally