PowerShell scripts usually start with logging as a few helpful messages:

Write-Host 'Starting script...'

That is enough while the script is small and you are sitting at the console watching it run. It becomes a different story when the script is scheduled, deployed remotely, or executing against hundreds of endpoints.

When something goes wrong later, the question is no longer, “What is the script doing?” It is, “What happened, when did it happen, and what led to this result?”

Useful production logging should provide a clear account of an execution. It should distinguish routine activity from warnings and failures, connect related messages across long or concurrent runs, and preserve enough context to explain decisions. It should also be easy for the person on the other end to read when they cannot reproduce the problem themselves.

Before implementing a Write-Log function, it is worth deciding what that log entry needs to contain. In this first article, we will look at log levels, correlation IDs, meaningful context, and the difference between useful operational history and excessive implementation detail.

Why Production Logging Is Different

Imagine a Datto RMM component running against 2,000 endpoints.

A customer reports that an application installation failed on one machine three days ago. The component has already finished, and all you have in the RMM job history is a short error message.

Something like this does not help much:

Installation failed.

Neither does this:

Something went wrong.

You know there was a failure, but almost nothing else.

Ideally, the local log contains something more useful:

2026-11-08 14:32:17.423 [Information] [c5fc38fe-76c0-45d4-bdf5-19a4633e3954] Starting application installation
2026-11-08 14:32:18.105 [Information] [c5fc38fe-76c0-45d4-bdf5-19a4633e3954] Download completed
2026-11-08 14:32:18.921 [Error] [c5fc38fe-76c0-45d4-bdf5-19a4633e3954] Installation failed with exit code 1603

That gives us much more information.

We know:

  1. when the failure occurred;
  2. what happened before the failure;
  3. which message represents the actual failure;
  4. which messages belong to the same execution.

Those are the fundamentals I want from production logging.

Let’s look at them individually.

Use Log Levels

Not every message has the same importance.

One of the first things I add to PowerShell logging is a concept of log levels.

For most automation, these four levels are sufficient:

LevelPurpose
DebugDetailed troubleshooting information
InformationNormal operational events
WarningAn unexpected situation that does not stop execution
ErrorA failure requiring attention

Instead of treating every message the same, I can write:

Write-Log -Level Debug -Message 'Checking registry path HKLM:\Software\Contoso'
Write-Log -Level Information -Message 'Application detected'
Write-Log -Level Warning -Message 'Configuration file does not exist'
Write-Log -Level Error -Message 'Application installation failed'

The distinction becomes increasingly important as the amount of automation grows.

If every message is an error, nothing is an error.

Debug

Debug messages contain information that helps troubleshoot a problem but normally does not require attention.

For example:

Detected operating system build 26100
Searching the registry for installed application
Installer arguments generated
Application version detected as 4.8.2

That information can be extremely useful when something goes wrong.

It can also create a lot of noise.

For that reason, I normally make Debug logging optional:

$script:EnableDebugLogging = $false

During normal operation, Debug messages can be excluded from the log or at least from the output presented to the operator.

When troubleshooting is required, Debug logging can be enabled.

Information

Information is what I consider the normal operational level.

For example:

Script started
Application detected
Configuration updated
Service restarted
Installation completed successfully

Most log entries during a healthy execution should probably be Information messages.

These entries tell the story of what the automation is doing without describing every individual PowerShell operation.

Warning

Warnings indicate something unexpected happened, but the script can continue.

For example:

Optional configuration file not found
Service was already stopped
Primary download source unavailable, trying fallback
Restart required to complete installation

A Warning should not automatically mean that the automation failed.

Consider:

Installation completed successfully
Restart required to complete installation

The installation itself succeeded, but there is still something an administrator may need to know.

That makes Warning a useful distinction between normal events and actual failures.

Error

Errors indicate a failure that requires attention.

For example:

Unable to download installer
Required service could not be started
Installation returned exit code 1603
API authentication failed

Once severity becomes a property of the log event, you can search specifically for:

Level = Error

instead of trying to determine whether arbitrary text looks like an error.

This becomes particularly useful once logging is structured and consumed by other systems.

Give Every Execution a Correlation ID

Timestamps tell us when something happened, but they do not necessarily tell us which execution caused it.

Consider a scheduled task that starts every five minutes.

Normally, the previous execution completes before the next one begins.

Until one day it doesn’t.

Now two executions overlap and write to the same log.

Which messages belong together?

This is where a correlation ID becomes extremely useful.

At the start of the script, generate a GUID:

$script:CorrelationId = [guid]::NewGuid().Guid

This produces an identifier similar to:

c5fc38fe-76c0-45d4-bdf5-19a4633e3954

Every log message written during that execution receives the same ID:

2026-11-08 14:32:17 [Information] [c5fc38fe-...] Script started
2026-11-08 14:32:18 [Information] [c5fc38fe-...] Application detected
2026-11-08 14:32:22 [Information] [c5fc38fe-...] Installation started
2026-11-08 14:32:36 [Error] [c5fc38fe-...] Installation failed

The next execution receives another ID:

2026-11-08 14:37:17 [Information] [a215774b-...] Script started

Now it does not matter if the executions overlap.

The correlation ID tells you which messages are related.

If necessary, you can extract one execution from a traditional text log with PowerShell:

Select-String -Path 'C:\ProgramData\Contoso\Logs\Automation.log' -Pattern $CorrelationId

The same concept becomes even more useful once the logger starts producing structured data.

Why Not Just Use the Process ID?

You might wonder why we need another identifier when PowerShell already has $PID.

A process ID is useful context and I normally include it, but it is not a replacement for a correlation ID.

Process IDs are reused by Windows.

A correlation ID is generated specifically to identify a logical execution of our automation.

The distinction also gives us more flexibility later. A correlation ID can potentially be passed between scripts or child operations while those operations use different process IDs.

So I generally record both.

Add Context to Every Log Entry

A message becomes much more useful when you know where it came from.

A basic log event might contain:

Timestamp
Level
CorrelationId
Message

For production automation, I usually want additional context:

ComputerName
ProcessId
ScriptName
ScriptVersion
UserName

Depending on the environment, you might also include:

ComponentName
Customer
Site
DeviceId

A structured log entry could eventually look something like this:

$LogEntry = [ordered]@{
    Timestamp     = (Get-Date).ToUniversalTime().ToString('o')
    Level         = 'Information'
    CorrelationId = $script:CorrelationId
    ComputerName  = $env:COMPUTERNAME
    UserName      = [Environment]::UserName
    ProcessId     = $PID
    ScriptName    = $script:ScriptName
    ScriptVersion = $script:ScriptVersion
    Message       = 'Application installation started'
}

Now imagine that log arriving in a central system without any other context.

You still know:

  • when the event occurred;
  • which computer generated it;
  • which execution generated it;
  • which process produced it;
  • which script generated it;
  • which version of that script was running;
  • what happened;
  • how important the event was.

That makes troubleshooting considerably easier.

Log Decisions, Not Implementation Details

A good production log tells the story of an execution.

It should not be a transcript of every line of PowerShell.

Consider an application upgrade.

Too Little

Started
Done

That tells us almost nothing.

If the upgrade fails, we have no idea how far the script got.

Too Much

At the opposite extreme:

Entering function Test-Application
Setting variable Path
Reading variable Path
Calling Test-Path
Test-Path returned true
Entering if statement
Calling Get-Item
Get-Item returned object

This tells us a lot about how the script was implemented, but very little about what it was trying to accomplish.

That type of information can sometimes be useful for debugging, but it should not dominate the normal production log.

Better

A much more useful log would look like this:

Script started
Installed version detected: 4.7.1
Target version: 4.8.2
Upgrade required
Downloading installer
Installer downloaded
Starting installation
Installation returned exit code 0
Installed version verified: 4.8.2
Script completed successfully

That tells the story.

An administrator troubleshooting the machine does not need to know that Test-Path returned $true.

They need to know why the script decided an upgrade was necessary.

Compare these two messages:

Version comparison returned -1

and:

Installed version 4.7.1 is older than required version 4.8.2. Upgrade required.

Both represent essentially the same internal decision.

The second one is much more useful six months later when someone else is troubleshooting the script.

That leads to one of my main logging rules:

Log decisions, not implementation details.

Log Important State Transitions

A useful way to decide what deserves an Information-level log entry is to look for state transitions.

For an application deployment, those might be:

Detection started
Application detected
Upgrade required
Download started
Download completed
Installation started
Installation completed
Verification started
Installed version verified
Script completed

These events describe movement through the automation.

If the log stops at:

Download completed
Installation started

you immediately know where to begin troubleshooting.

You do not need every variable assignment between those two events.

Debug logging can provide those details if they become necessary.

Think About the Person Reading the Log

When writing logging messages, it is easy to think only about the script you are currently building.

You know exactly how the script works.

The person reading the log six months from now might not.

That person may even be you.

Consider:

Configuration invalid.

Which configuration?

Why is it invalid?

What did the script expect?

This is better:

Configured installation path 'C:\Contoso' does not exist.

Or:

Configured version '4.7.1' is lower than required version '4.8.2'.

The message should provide enough context to make the event useful without requiring someone to immediately open the script and reverse-engineer what happened.

At the same time, be careful about what that context contains.

Passwords, API keys, access tokens, authorization headers, and other secrets should never be intentionally written to a log.

We will address secure logging separately later in this series because it deserves more attention than a short warning.

What About PowerShell Transcripts?

PowerShell already provides Start-Transcript, so it is reasonable to ask why we need another logging mechanism.

A transcript is extremely useful:

Start-Transcript -Path 'C:\ProgramData\Contoso\Logs\Transcript.log'

Get-Service
Get-Process

Stop-Transcript

But a transcript and an application log solve different problems.

A transcript captures activity from the PowerShell session.

An application logger records events that we deliberately define as operationally meaningful.

For example, a transcript might show the commands involved in determining an installed application version.

The application log would simply say:

Installed version detected: 4.7.1

That is the information an operator usually wants.

I do not consider transcripts and structured logging competitors.

I see them as different tools:

MethodPurpose
Application loggingNormal operational logging
PowerShell transcriptDetailed troubleshooting

For everyday production automation, I want the application log.

If I need to investigate a particularly complicated execution problem, I might temporarily enable a transcript as well.

Designing the Log Entry

We now have enough information to define what our production log entry should eventually contain.

At minimum:

Timestamp
Level
CorrelationId
Message

For most of my automation, I would also add:

ComputerName
ProcessId

And depending on the script:

ScriptName
ScriptVersion
UserName
ComponentName

Conceptually, our event now looks like this:

Timestamp     : 2026-11-08T13:32:17.4230000Z
Level         : Information
CorrelationId : c5fc38fe-76c0-45d4-bdf5-19a4633e3954
ComputerName  : PC001
ProcessId     : 4812
ScriptName    : ApplicationDeployment.ps1
ScriptVersion : 1.2.0
Message       : Installed version 4.7.1 detected

At this point, we have not really built a logging framework yet.

That is deliberate.

Before writing the function, we have defined what the function needs to accomplish.

Conclusion

Good PowerShell logging is not about putting Write-Host everywhere.

It is about recording enough information to reconstruct what the automation did when something behaves differently from what you expected.

For most production scripts, I want every log event to answer some combination of these questions:

  • When did it happen?
  • What happened?
  • How important was it?
  • Which execution did it belong to?
  • Where did it happen?
  • What decision did the automation make?

That leads to a few straightforward rules:

  1. Use clear Debug, Information, Warning, and Error levels.
  2. Give every execution a correlation ID.
  3. Add useful context such as the computer name and process ID.
  4. Log state transitions and decisions, not every implementation detail.
  5. Keep Debug logging available for the details you do not normally need.
  6. Write messages for the person troubleshooting the script six months from now.

Once those concepts are in place, we can start implementing them.

In Part 2, we’ll build a reusable Write-Log function that turns these ideas into both human-readable text logs and structured JSON Lines output.

Leave a Reply

Your email address will not be published. Required fields are marked *