PowerShell Fundamentals

Errors Logging and WhatIf

Handle PowerShell errors with correct categories, try and catch, structured logs, WhatIf previews, and confirmation boundaries.

Beginner14 min read
PowerShell Fundamentals lessonAutomation foundationsLearn

Handle PowerShell errors with correct categories, try and catch, structured logs, WhatIf previews, and confirmation boundaries.

What you will be able to do

  • Explain the five core programming decisions involved in errors logging and whatif.
  • Choose a small code or command method that matches a stated input and output need.
  • Interpret execution output and errors before changing broader system state.
  • Apply an inspect, implement, test, verify, and recover workflow to a realistic automation task.

01

Build the Execution Model

Handle PowerShell errors with correct categories, try and catch, structured logs, WhatIf previews, and confirmation boundaries.

Reliable automation separates input, representation, decision, action, output, and failure behavior. Before writing a longer script, state the exact starting data, the intended transformation, the observable result, and which state the program is permitted to change.

Use a disposable project and small deterministic samples. Run read-only inspection first, keep the interpreter or shell and working directory visible, and preserve the exact input with its output. A reproducible example is more useful than a large script whose state is unknown.

02

Error categories

PowerShell distinguishes non-terminating, statement-terminating, and script-terminating errors. It is one distinct part of errors logging and whatif, so keep its input and output visible instead of hiding them inside a larger unexplained script.

Identify the category before choosing ErrorAction or try and catch. Start with this small method: Trigger one controlled non-terminating error. Record the code, interpreter or shell version, working directory, sample input, and visible result together.

The safety boundary is clear: A non-terminating error may let the pipeline continue. The expected evidence is The error categories result matches the documented input and expected state. If the result differs, keep the failure and reduce the example before changing unrelated code or system state.

03

Try catch finally

Try protects terminating work, catch handles matching errors, and finally performs cleanup. It is one distinct part of errors logging and whatif, so keep its input and output visible instead of hiding them inside a larger unexplained script.

Convert an expected non-terminating error when catch must handle it. Start with this small method: Use `-ErrorAction Stop` in a test. Record the code, interpreter or shell version, working directory, sample input, and visible result together.

The safety boundary is clear: A broad catch can hide unrelated failures. The expected evidence is The try catch finally result matches the documented input and expected state. If the result differs, keep the failure and reduce the example before changing unrelated code or system state.

04

Operational logging

Transcripts or structured records should capture action, target, result, and error context. It is one distinct part of errors logging and whatif, so keep its input and output visible instead of hiding them inside a larger unexplained script.

Write useful context without secrets or full sensitive objects. Start with this small method: Emit a timestamped result object. Record the code, interpreter or shell version, working directory, sample input, and visible result together.

The safety boundary is clear: Verbose output is not automatically a durable log. The expected evidence is The operational logging result matches the documented input and expected state. If the result differs, keep the failure and reduce the example before changing unrelated code or system state.

05

WhatIf preview

ShouldProcess enables WhatIf to describe a supported state-changing action without applying it. It is one distinct part of errors logging and whatif, so keep its input and output visible instead of hiding them inside a larger unexplained script.

Use the same target and action text for preview and execution. Start with this small method: Run an approved function with `-WhatIf`. Record the code, interpreter or shell version, working directory, sample input, and visible result together.

The safety boundary is clear: A custom preview that skips selection logic can be misleading. The expected evidence is The whatif preview result matches the documented input and expected state. If the result differs, keep the failure and reduce the example before changing unrelated code or system state.

06

Confirm impact

Confirm behavior asks for approval according to impact and preference settings. It is one distinct part of errors logging and whatif, so keep its input and output visible instead of hiding them inside a larger unexplained script.

Set ConfirmImpact appropriately and show the exact target. Start with this small method: Test confirmation only in a disposable scope. Record the code, interpreter or shell version, working directory, sample input, and visible result together.

The safety boundary is clear: Interactive prompts can block unattended automation. The expected evidence is The confirm impact result matches the documented input and expected state. If the result differs, keep the failure and reduce the example before changing unrelated code or system state.

07

Implement One Small Behavior

Translate the requirement into one input-output example before implementation. Name the target and side effects explicitly, validate input at the program boundary, and keep secrets and environment-specific configuration outside source code and ordinary logs.

Add the smallest code that makes the example work. Inspect intermediate values as structured data, not only formatted display text. When an operation can modify files, accounts, services, remote systems, or API resources, build a preview or disposable test path first.

Handle the expected failure that belongs to the operation. Preserve its type, message, target, and timing, then return a meaningful result or nonzero exit. Do not catch errors the program cannot explain merely to make the run appear successful.

08

Test and Handoff the Automation

Test a normal input, one boundary input, and one safe failure. Compare actual output with the written expectation and verify any changed state through an independent read-only query. A zero exit code or successful request is only part of the evidence.

Make the run reproducible. Record runtime version, dependencies, parameters, configuration source, sample data, output shape, and cleanup or reversal procedure. Remove temporary credentials, test files, sessions, and permissions after verification.

A useful handoff explains what the program accepts, what it returns or changes, how it reports failure, and where execution must stop for review. Escalate when authorization, data ownership, target scope, vendor contract, or safe recovery remains uncertain.

09

Recap Before Practice and Prove

Start with error categories. Identify the category before choosing ErrorAction or try and catch. Verify that the error categories result matches the documented input and expected state.

Keep try catch finally explicit. Convert an expected non-terminating error when catch must handle it. Respect this boundary: a broad catch can hide unrelated failures..

Use operational logging as a separate decision. Write useful context without secrets or full sensitive objects. Preserve the exact input and output.

Before expanding the script, review whatif preview. Use the same target and action text for preview and execution. Stop when a custom preview that skips selection logic can be misleading..

Finish with confirm impact. Set ConfirmImpact appropriately and show the exact target. Record the final result, failure behavior, cleanup, and reproducible next step.

NEXT STEP

Turn reading into recall

Practice the concepts without a timer, with coaching and retry available after every answer.

Open guided practice