Skip to content

Extracting Log Files with PowerShell

This article explains how to extract specific content from log files using PowerShell, enabling efficient log analysis and system monitoring.

In large environments, log files can grow quickly and contain a vast amount of information. PowerShell offers powerful text-processing tools to filter relevant data, detect issues, and automate reporting tasks, making it an essential skill for system administrators.

Use Get-Content to read a log file line-by-line.

Terminal window
Get-Content -Path "C:\Logs\system.log"

Use Select-String to extract only lines containing specific keywords such as “ERROR”.

Terminal window
Get-Content -Path "C:\Logs\system.log" | Select-String -Pattern "ERROR"

Use an array of patterns to search for multiple keywords.

Terminal window
$patterns = "ERROR", "WARNING"
Get-Content -Path "C:\Logs\system.log" | Select-String -Pattern $patterns

Step 4: Extracting Logs Within a Date Range

Section titled “Step 4: Extracting Logs Within a Date Range”

Filter lines by date using a regular expression.

Terminal window
Get-Content -Path "C:\Logs\system.log" | Where-Object { $_ -match "2025-04-(1[5-9]|2[0-5])" }

Save filtered log data to a CSV file for reporting.

Terminal window
Get-Content -Path "C:\Logs\system.log" | Select-String "ERROR" | ForEach-Object {
[PSCustomObject]@{
LineNumber = $_.LineNumber
Text = $_.Line
Path = $_.Path
}
} | Export-Csv -Path "C:\Logs\error_report.csv" -NoTypeInformation

Wrap it all in a reusable .ps1 script for daily use.

Terminal window
$logPath = "C:\Logs\system.log"
$outputPath = "C:\Logs\filtered_log.csv"
$filterPattern = "ERROR"
Get-Content -Path $logPath | Select-String -Pattern $filterPattern | ForEach-Object {
[PSCustomObject]@{
LineNumber = $_.LineNumber
Text = $_.Line
Path = $_.Path
}
} | Export-Csv -Path $outputPath -NoTypeInformation

With just a few lines of PowerShell, you can efficiently extract and manage critical log information. Whether monitoring for errors or generating daily summaries, scripting log analysis saves time and increases system visibility.