Event logs help you see what happened on your computer. Reading them lets you find problems or check important actions.
Event log reading in PowerShell
Introduction
Syntax
PowerShell
Get-EventLog -LogName <LogName> [-Newest <Number>] [-EntryType <Type>] [-After <DateTime>] [-Before <DateTime>]
-LogName is the name of the event log, like 'System' or 'Application'.
You can filter events by type (Error, Warning, Information) or by date.
Examples
PowerShell
Get-EventLog -LogName System -Newest 5PowerShell
Get-EventLog -LogName Application -EntryType Error -After (Get-Date).AddDays(-1)PowerShell
Get-EventLog -LogName Security -Newest 10Sample Program
This script shows the last 3 error events from the System log. It prints the time, source, and message for each event.
PowerShell
Write-Host "Last 3 errors from System log:"; Get-EventLog -LogName System -EntryType Error -Newest 3 | ForEach-Object { Write-Host "Time:" $_.TimeGenerated; Write-Host "Source:" $_.Source; Write-Host "Message:" $_.Message; Write-Host "---"; }
Important Notes
You need to run PowerShell as Administrator to read some logs like Security.
Event logs can be large; filtering helps find what you need faster.
Use Get-EventLog -List to see all available logs on your system.
Summary
Event log reading helps find system or application issues.
Use Get-EventLog with filters to get specific events.
Always check the time, source, and message to understand each event.
Practice
1. What does the PowerShell cmdlet
Get-EventLog primarily do?easy
Solution
Step 1: Understand the purpose of Get-EventLog
The cmdlet is designed to read and retrieve event log entries from Windows logs.Step 2: Compare with other options
Creating, deleting, or updating logs are not functions of Get-EventLog; it only reads logs.Final Answer:
It retrieves entries from Windows event logs. -> Option BQuick Check:
Get-EventLog reads logs = A [OK]
Hint: Get-EventLog always reads logs, not modifies them [OK]
Common Mistakes:
- Confusing reading logs with creating or deleting logs
- Thinking it modifies event log settings
- Assuming it works for non-Windows logs
2. Which of the following is the correct syntax to get the last 10 entries from the System event log in PowerShell?
easy
Solution
Step 1: Identify correct parameter for log name
The parameter to specify the log is '-LogName', so Get-EventLog -Log System -Last 10 is incorrect because it uses '-Log'.Step 2: Identify correct parameter for number of entries
The correct parameter to get recent entries is '-Last', not '-Newest' or '-Top'.Final Answer:
Get-EventLog -LogName System -Last 10 -> Option AQuick Check:
Use -LogName and -Last for recent entries [OK]
Hint: Use -LogName and -Last to get recent events [OK]
Common Mistakes:
- Using -Log instead of -LogName
- Using -Newest or -Top which are invalid parameters
- Mixing parameter names
3. What will be the output of this PowerShell command?
Get-EventLog -LogName Application -EntryType Error -Newest 2 | Select-Object -Property TimeGenerated, Source
medium
Solution
Step 1: Analyze Get-EventLog parameters
The command filters Application log entries to only 'Error' type and selects the newest 2 entries.Step 2: Understand Select-Object usage
Select-Object limits output to only TimeGenerated and Source properties for those entries.Final Answer:
The two most recent error events from the Application log showing their time and source. -> Option CQuick Check:
Filters + selects properties = recent errors with time and source [OK]
Hint: Newest + EntryType filters recent errors; Select-Object picks fields [OK]
Common Mistakes:
- Thinking it shows all events, not filtered
- Confusing newest with oldest entries
- Believing Select-Object causes errors here
4. You run this command but get an error:
What is the most likely cause?
Get-EventLog -LogName Security -EntryType Warning
What is the most likely cause?
medium
Solution
Step 1: Understand Security log restrictions
The Security log often does not support filtering by EntryType Warning because it mainly contains Audit Success or Failure events.Step 2: Check parameter correctness and usage
The parameter is spelled correctly and Get-EventLog can read Security logs, so those are not causes.Final Answer:
The Security log does not support filtering by EntryType Warning. -> Option AQuick Check:
Security log limits EntryType filters = C [OK]
Hint: Security log has limited EntryType filters, no Warning [OK]
Common Mistakes:
- Assuming EntryType is misspelled
- Thinking Get-EventLog can't read Security log
- Believing -Newest is required with -EntryType
5. You want to find all error events from the System log in the last 24 hours and export their TimeGenerated, Source, and Message to a CSV file. Which script correctly does this?
hard
Solution
Step 1: Filter errors and time correctly
Get-EventLog -LogName System -EntryType Error | Where-Object { $_.TimeGenerated -gt (Get-Date).AddDays(-1) } | Select-Object TimeGenerated, Source, Message | Export-Csv -Path errors.csv -NoTypeInformation uses Get-EventLog with EntryType Error, then filters events generated within last 24 hours using Where-Object and Get-Date().AddDays(-1).Step 2: Select needed properties and export
It selects TimeGenerated, Source, and Message, then exports to CSV with -NoTypeInformation to avoid extra type info.Step 3: Check other options for errors
Get-EventLog -LogName System -EntryType Error -After (Get-Date).AddDays(-1) | Select-Object TimeGenerated, Source, Message | Export-Csv errors.csv uses invalid -After parameter (not supported by Get-EventLog). Get-EventLog -LogName System -EntryType Error -Newest 24 | Select TimeGenerated, Source, Message | Export-Csv -Path errors.csv uses -Newest 24 which gets last 24 entries, not last 24 hours. Get-EventLog -LogName System | Where-Object { $_.EntryType -eq 'Error' -and $_.TimeGenerated -lt (Get-Date).AddDays(-1) } | Export-Csv -Path errors.csv filters for events older than 24 hours (-lt), opposite of requirement.Final Answer:
Get-EventLog -LogName System -EntryType Error | Where-Object { $_.TimeGenerated -gt (Get-Date).AddDays(-1) } | Select-Object TimeGenerated, Source, Message | Export-Csv -Path errors.csv -NoTypeInformation -> Option DQuick Check:
Filter by EntryType + Where-Object time + Select + Export-Csv = A [OK]
Hint: Use Where-Object with TimeGenerated for date filtering [OK]
Common Mistakes:
- Using unsupported -After parameter with Get-EventLog
- Confusing -Newest with time filtering
- Filtering with wrong time comparison operator
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
