-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-BootShutdownEvents.ps1
More file actions
159 lines (139 loc) · 6.56 KB
/
Copy pathGet-BootShutdownEvents.ps1
File metadata and controls
159 lines (139 loc) · 6.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<#
.SYNOPSIS
Shows a compact, admin-friendly timeline of recent boots, shutdowns,
crashes, and update-related restart signals from the System log.
.DESCRIPTION
Use this for first-pass reboot and shutdown triage. It helps answer
whether a recent restart was clean, unexpected, crash-related, planned
by an administrator or process, or associated with Windows Updates.
.OUTPUTS
Produces one PSCustomObject per matching event:
Level : Event level text.
Time : Event time.
Kind : Event category. One of:
Boot, NormalShutdown, PlannedShutdownOrRestart,
WindowsUpdatesInstallStarted, WindowsUpdatesInstallCompleted,
WindowsUpdatesRestartRequired, WindowsUpdatesRestart
BugCheck, CrashDump, AbnormalShutdown,
UnexpectedShutdownFollowup, Other
Description : First line of the event message with source metadata
appended as "(id <n> from <provider>)".
Intentionally excludes noise about Microsoft Defender updates.
.EXAMPLE
PS C:\> Get-BootShutdownEvents |ft
Level Time Kind Description
----- ---- ---- -----------
Info 15/3 6:00:47 WindowsUpdatesInstallStart Installation Started: Windows has started installing the following update: 2026-03 Cumulative Update for Microsoft ...
Info 15/3 6:19:54 WindowsUpdatesRestart The process C:\WINDOWS\system32\shutdown.exe (SRV2) has initiated the restart of computer SRV2 ...
Info 15/3 6:22:06 NormalShutdown The Event log service was stopped. (id 6006 from EventLog)
Info 15/3 6:22:12 NormalShutdown The operating system is shutting down at system time 2026 - 03 - 15T04:22:12.144072600Z. (id 13 from Microsoft-Windows-Kernel-General)
Info 15/3 6:22:14 Boot The operating system started at system time 2026 - 03 - 15T04:22:14.500000000Z. (id 12 from Microsoft-Windows-Kernel-General)
Info 15/3 6:22:22 Boot The Event log service was started. (id 6005 from EventLog)
Info 15/3 6:22:44 WindowsUpdatesRestart The process C:\WINDOWS\servicing\TrustedInstaller.exe (SRV2) has initiated the restart of computer SRV2...
Info 15/3 6:22:45 NormalShutdown The Event log service was stopped. (id 6006 from EventLog)
Info 15/3 6:22:51 NormalShutdown The operating system is shutting down at system time 2026 - 03 - 15T04:22:51.294803800Z. (id 13 from Microsoft-Windows-Kernel-General)
Info 15/3 6:22:53 Boot The operating system started at system time 2026 - 03 - 15T04:22:53.500000000Z. (id 12 from Microsoft-Windows-Kernel-General)
Info 15/3 6:22:59 Boot The Event log service was started. (id 6005 from EventLog)
Info 15/3 6:23:20 WindowsUpdatesInstallCompl Installation Successful: Windows successfully installed the following update: 2026-03 Cumulative Update for Microsoft ...
#>
[CmdletBinding()]
param(
[ValidateRange(1, 720)]
[int]$LastHours = 48,
[switch]$NewestFirst
)
$startTimeUtc = (Get-Date).AddHours(-$LastHours).ToUniversalTime().ToString('s') + '.000Z'
$filterXml = @"
<QueryList>
<Query Id="0" Path="System">
<Select Path="System">
*[
System[
TimeCreated[@SystemTime >= '$startTimeUtc']
and
(
(Provider[@Name='Microsoft-Windows-Kernel-General'] and (EventID=12 or EventID=13))
or
(Provider[@Name='Microsoft-Windows-Kernel-Power'] and EventID=41)
or
(Provider[@Name='User32'] and (EventID=1074 or EventID=1076))
or
(Provider[@Name='EventLog'] and (EventID=6005 or EventID=6006 or EventID=6008))
or
((Provider[@Name='Microsoft-Windows-WER-SystemErrorReporting'] or Provider[@Name='BugCheck']) and EventID=1001)
or
(Provider[@Name='volmgr'] and (EventID=46 or EventID=161))
or
(Provider[@Name='Microsoft-Windows-WindowsUpdateClient'] and (EventID=19 or EventID=21 or EventID=43))
)
]
]
</Select>
</Query>
</QueryList>
"@
$events = New-Object 'System.Collections.Generic.List[object]'
foreach ($evt in Get-WinEvent -FilterXml $filterXml -ErrorAction Stop) {
$message = $evt.Message
$description = if ([string]::IsNullOrWhiteSpace($message)) {
''
} else {
($message -split "`r?`n", 2)[0]
}
if ($description -match '\bKB2267602\b') {
continue
}
$null = $events.Add([pscustomobject]@{
Event = $evt
ProviderName = $evt.ProviderName
Id = $evt.Id
LevelDisplayName = $evt.LevelDisplayName
Message = $message
Description = $description
})
}
$sortedEvents = $events |
Sort-Object `
@{ Expression = { $_.Event.TimeCreated }; Descending = $NewestFirst }, `
@{ Expression = { $_.Event.RecordId }; Descending = $NewestFirst }
foreach ($item in $sortedEvents) {
$providerName = $item.ProviderName
$id = $item.Id
$message = $item.Message
$key = "$($providerName)|$($id)"
$kind = switch ($key) {
'Microsoft-Windows-Kernel-General|12' { 'Boot'; break }
'Microsoft-Windows-Kernel-General|13' { 'NormalShutdown'; break }
'Microsoft-Windows-Kernel-Power|41' { 'AbnormalShutdown'; break }
'User32|1074' {
if ($message -match 'TrustedInstaller|Windows Update|Operating System: Upgrade') {
'WindowsUpdatesRestart'
} else {
'PlannedShutdownOrRestart'
}
break
}
'User32|1076' { 'UnexpectedShutdownFollowup'; break }
'EventLog|6005' { 'Boot'; break }
'EventLog|6006' { 'NormalShutdown'; break }
'EventLog|6008' { 'AbnormalShutdown'; break }
'Microsoft-Windows-WER-SystemErrorReporting|1001' { 'BugCheck'; break }
'BugCheck|1001' { 'BugCheck'; break }
'volmgr|46' { 'CrashDump'; break }
'volmgr|161' { 'CrashDump'; break }
'Microsoft-Windows-WindowsUpdateClient|43' { 'WindowsUpdatesInstallStarted'; break }
'Microsoft-Windows-WindowsUpdateClient|19' { 'WindowsUpdatesInstallCompleted'; break }
'Microsoft-Windows-WindowsUpdateClient|21' { 'WindowsUpdatesRestartRequired'; break }
default { 'Other' }
}
[pscustomobject]@{
Level = $item.LevelDisplayName
Time = $item.Event.TimeCreated
Kind = $kind
Description = if ([string]::IsNullOrWhiteSpace($item.Description)) {
"(id $($id) from $($providerName))"
} else {
"$($item.Description) (id $($id) from $($providerName))"
}
}
}