Windows Server 2025 / How to Handle PowerShell Errors and Write Run Logs

A completed command is not necessarily a successful administrative task. PowerShell try, catch, finally, explicit error handling, and per-run logs help identify failures and show when only part of a task finished.

Make failures visible

This exercise reads a file without changing server roles. A real installation or account-management script must also record changes left by an intermediate failure. Entering catch does not automatically undo earlier actions.

Some PowerShell errors allow execution to continue. Merely placing a command inside try does not make every error catchable; -ErrorAction Stop promotes supported errors to terminating errors. Review PowerShell error handling for the distinction.

Prepare an input file and the complete script

Create C:\LabScripts\input.txt containing one line, lab ready. Save the complete code below as Read-LabFile.ps1 in the same folder using UTF-8. The Windows PowerShell 5.1 example reads files and writes logs; it does not change server services or networking.

param(
    [Parameter(Mandatory=$true)]
    [string]$InputPath
)
$logDirectory = 'C:\LabLogs'
New-Item -ItemType Directory -Path $logDirectory -Force -ErrorAction Stop | Out-Null
$runLabel = (Get-Date -Format 'yyyyMMdd-HHmmss') + '-' + ([guid]::NewGuid().ToString('N').Substring(0,6))
$logPath = Join-Path $logDirectory ($runLabel + '.json')
$result = [ordered]@{
    StartedAt = (Get-Date).ToString('o')
    ComputerName = $env:COMPUTERNAME
    InputPath = $InputPath
    Success = $false
    LineCount = $null
    ErrorId = $null
    ErrorMessage = $null
}
$exitStatus = 0
try {
    $lines = @(Get-Content -LiteralPath $InputPath -ErrorAction Stop)
    $result.LineCount = $lines.Count
    $result.Success = $true
}
catch {
    $result.ErrorId = $_.FullyQualifiedErrorId
    $result.ErrorMessage = $_.Exception.Message
    $exitStatus = 1
    Write-Warning $result.ErrorMessage
}
finally {
    $result.FinishedAt = (Get-Date).ToString('o')
    $result | ConvertTo-Json | Set-Content -LiteralPath $logPath -Encoding UTF8 -ErrorAction Stop
}
Write-Output $logPath
exit $exitStatus

Compare success and failure under the same conditions

The first invocation reads the existing file. The second uses missing-input.txt, which must genuinely be absent. You do not need to delete the input or modify a system file to create a failure:

powershell.exe -NoProfile -File 'C:\LabScripts\Read-LabFile.ps1' -InputPath 'C:\LabScripts\input.txt'
$LASTEXITCODE
powershell.exe -NoProfile -File 'C:\LabScripts\Read-LabFile.ps1' -InputPath 'C:\LabScripts\missing-input.txt'
$LASTEXITCODE

Immediately inspect each exit code. The expected success case returns 0 with Success=true. The missing file returns 1 with Success=false and error details. Each invocation creates a different JSON file; open the reported paths and compare InputPath and ErrorId.

If script policy blocks execution, follow the organization’s policy and signing requirements rather than weakening global settings for this example.

Understand each error-handling section

Section Purpose in this script
try Read the file and record its line count
catch Record the handled error identifier and message; select a failure exit code
finally Attempt to save the run result regardless of read success

Microsoft’s try, catch, and finally reference explains terminating-error handling. A finally block cannot guarantee a writable log disk. Full storage or denied access can still prevent saving, so log writes also use -ErrorAction Stop. Do not report success simply because no log exists.

External programs return their own exit codes. In Windows PowerShell 5.1, do not assume a nonzero exit automatically enters catch. Inspect $LASTEXITCODE immediately and throw when appropriate; another external command can replace that value.

Add safe rerun criteria to operational scripts

  • Record the server, target, and start and finish times.
  • Query existing state to avoid duplicate creation or unintended overwrite.
  • Track successful and failed steps separately.
  • Keep passwords, tokens, and unnecessary user-file contents out of logs.
  • If implementing rollback, record its supported scope and any recovery failure.

Restore the valid input path and run again to confirm a new successful result. Preserve needed evidence before removing only the temporary files from this lab. Adding SilentlyContinue everywhere hides failures rather than making administration reliable.

More posts in this category
Windows Server 2025 / How to Install Windows Admin Center and Connect a Server

Windows Server 2025 / How to Install Windows Admin Center and Connect a Server

Plan the gateway, HTTPS certificate, and server connection, then troubleshoot browser and management paths separately.

Windows Server 2025 / How to Install Desktop Experience

Windows Server 2025 / How to Install Desktop Experience

Select the correct installation image and disk, complete setup in a lab VM, and verify Administrator access and operating system details.

Windows Server 2025 / How to Enable Remote Desktop and Check Firewall Access

Windows Server 2025 / How to Enable Remote Desktop and Check Firewall Access

Configure administrative RDP access with NLA, scoped firewall rules, and tests that separate network reachability from user authentication.

Windows Server 2025 / How to Handle PowerShell Errors and Write Run Logs

Windows Server 2025 / How to Handle PowerShell Errors and Write Run Logs

Compare successful and failed file reads, preserve structured error details, and distinguish exception handling from rollback.

Windows Server 2025 / How to Export Server and Disk Inventory to CSV

Windows Server 2025 / How to Export Server and Disk Inventory to CSV

Collect consistent local inventory into separate run folders, verify both CSV files, and compare values with their units and collection time.

Windows Server 2025 / How to Use PowerShell Remoting with WinRM

Windows Server 2025 / How to Use PowerShell Remoting with WinRM

Configure a targeted remote management path, distinguish domain and workgroup authentication, and verify where commands execute.

Windows Server 2025 / DNS Basics: Zones, Records, and Name Resolution

Windows Server 2025 / DNS Basics: Zones, Records, and Name Resolution

Understand the records and zones used in the lab, distinguish cached answers from current data, and separate DNS from service access.

Windows Server 2025 / Server Core vs Desktop Experience: Which Should You Install?

Windows Server 2025 / Server Core vs Desktop Experience: Which Should You Install?

Compare local tools, remote management, application support, and recovery access before choosing a Windows Server 2025 installation option.

Windows Server 2025 / Standard vs Datacenter: How to Choose an Edition

Windows Server 2025 / Standard vs Datacenter: How to Choose an Edition

Compare editions separately from installation options, review feature and virtualization requirements, and check the edition installed on your server.

Windows Server 2025 / How to Manage Local Users and Groups

Windows Server 2025 / How to Manage Local Users and Groups

Create standard local accounts and groups, verify membership and session identity, and understand safe disablement and cleanup.