GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Windows-PowerShell-Scripts/Diagnose-StartupAndPolicy.ps1 at main · Ryan-Adams57/Windows-PowerShell-Scripts

Expand file tree

/

Diagnose-StartupAndPolicy.ps1

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
320 lines (292 loc) · 13.1 KB
/

Diagnose-StartupAndPolicy.ps1

Copy path

File metadata and controls

320 lines (292 loc) · 13.1 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<#
Diagnose-StartupAndPolicy.ps1
SCRIPT HIGHLIGHTS
- Read-only. Makes no changes to the system. Safe to run at any time.
- Finds what triggers the "managed by your organization" banner in Chrome and Edge:
names the exact policy values and whether they come from local registry or the cloud.
- Lists everything that launches at logon or boot (scheduled tasks, Run keys, Startup folders).
- Flags scheduled tasks whose target .ps1/.bat/.cmd no longer exists. That is the usual
cause of a stray CMD or PowerShell window popping up on every restart.
- Reports whether the device is actually Azure AD joined or MDM enrolled, so you can tell
real management apart from a leftover policy key.
- Writes a timestamped text report and can open it with -OpenReport.
REQUIREMENTS
- Windows PowerShell 5.1 or later. No external modules.
- Run as Administrator for complete results (HKLM reads and some task details need elevation).
PARAMETERS
- ExportPath : full path for the report file. Defaults to a timestamped file on the Desktop.
- OpenReport : switch. Opens the finished report in Notepad. Off by default (automation-safe).
DISCLAIMER
- Provided as-is with no warranty. Read-only by design: it reports, it does not remediate.
Review every finding before acting on it. The browser banner and the PowerShell 5.1
"install the latest" notice are not bugs; see the README for what each one means.
Last Updated: 2026-11-21
#>
[CmdletBinding()]
param(
[Parameter()]
[string]$ExportPath,
[Parameter()]
[switch]$OpenReport
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
# ---------------------------------------------------------------------------
# Setup: resolve a default report path and prepare the in-memory report buffer
# ---------------------------------------------------------------------------
if (-not $ExportPath) {
try {
# GetFolderPath respects OneDrive Desktop redirection; USERPROFILE does not.
$desktop = [Environment]::GetFolderPath('Desktop')
if (-not $desktop) { $desktop = $env:USERPROFILE }
}
catch {
$desktop = $env:USERPROFILE
}
$stamp = Get-Date -Format 'yyyyMMdd_HHmmss'
$ExportPath = Join-Path $desktop ("StartupPolicyReport_{0}.txt" -f $stamp)
}
# Collects every printed line so the same content lands in the report file.
$report = New-Object System.Collections.Generic.List[string]
function Add-Line {
param(
[string]$Text,
[string]$Color
)
if ($Color) { Write-Host $Text -ForegroundColor $Color }
else { Write-Host $Text }
$report.Add($Text)
}
# Dumps every value and subkey under a registry path, or notes its absence.
function Show-PolicyKey {
param(
[string]$Path,
[string]$Label
)
if (Test-Path $Path) {
Add-Line ("[{0}] {1}" -f $Label, $Path) 'Yellow'
try {
$item = Get-ItemProperty -Path $Path -ErrorAction Stop
$props = $item.PSObject.Properties | Where-Object { $_.Name -notlike 'PS*' }
if ($props) {
foreach ($p in $props) { Add-Line (" {0} = {1}" -f $p.Name, $p.Value) }
}
else {
Add-Line " (key exists with no values - its mere presence can trigger the banner)"
}
$subs = Get-ChildItem -Path $Path -Recurse -ErrorAction SilentlyContinue
foreach ($s in $subs) { Add-Line (" SUBKEY: {0}" -f $s.PSChildName) }
}
catch {
Add-Line (" ERROR reading key: {0}" -f $_.Exception.Message) 'Red'
}
}
else {
Add-Line ("[{0}] {1} -> not present" -f $Label, $Path)
}
}
# Pulls the first .ps1/.bat/.cmd path out of a task's command line, if any.
function Resolve-TaskFile {
param([string]$Full)
$m = [regex]::Match($Full, '"([^"]+\.(?:ps1|bat|cmd))"')
if ($m.Success) { return $m.Groups[1].Value }
$m = [regex]::Match($Full, '([A-Za-z]:\\[^\s"]+\.(?:ps1|bat|cmd))')
if ($m.Success) { return $m.Groups[1].Value }
return ''
}
# ---------------------------------------------------------------------------
# Header + elevation check
# ---------------------------------------------------------------------------
Add-Line ("Startup and Policy Diagnostic - {0}" -f (Get-Date)) 'Cyan'
Add-Line ("Computer: {0} User: {1}" -f $env:COMPUTERNAME, $env:USERNAME)
Add-Line ""
try {
$principal = New-Object Security.Principal.WindowsPrincipal(
[Security.Principal.WindowsIdentity]::GetCurrent())
$isAdmin = $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
}
catch {
$isAdmin = $false
}
if (-not $isAdmin) {
Add-Line "WARNING: not elevated. Some HKLM keys and task details may be incomplete. Re-run as Administrator for a full picture." 'Yellow'
Add-Line ""
}
$totalSteps = 7
$step = 0
# ---------------------------------------------------------------------------
# 1. Browser policy keys (the "managed by your organization" source)
# ---------------------------------------------------------------------------
$step++; Write-Progress -Activity "Diagnosing" -Status "Browser policies" -PercentComplete (($step / $totalSteps) * 100)
Add-Line "===== 1. BROWSER POLICY KEYS =====" 'Cyan'
try {
Show-PolicyKey 'HKLM:\SOFTWARE\Policies\Google\Chrome' 'Chrome HKLM'
Show-PolicyKey 'HKLM:\SOFTWARE\WOW6432Node\Policies\Google\Chrome' 'Chrome HKLM x86'
Show-PolicyKey 'HKCU:\SOFTWARE\Policies\Google\Chrome' 'Chrome HKCU'
Show-PolicyKey 'HKLM:\SOFTWARE\Policies\Microsoft\Edge' 'Edge HKLM'
Show-PolicyKey 'HKLM:\SOFTWARE\WOW6432Node\Policies\Microsoft\Edge' 'Edge HKLM x86'
Show-PolicyKey 'HKCU:\SOFTWARE\Policies\Microsoft\Edge' 'Edge HKCU'
}
catch {
Add-Line (" ERROR in section 1: {0}" -f $_.Exception.Message) 'Red'
}
Add-Line ""
# ---------------------------------------------------------------------------
# 2. Chrome cloud-management enrollment token (banner with empty policy page)
# ---------------------------------------------------------------------------
$step++; Write-Progress -Activity "Diagnosing" -Status "Cloud enrollment" -PercentComplete (($step / $totalSteps) * 100)
Add-Line "===== 2. CHROME CLOUD ENROLLMENT TOKEN =====" 'Cyan'
try {
$chromeKey = Get-ItemProperty 'HKLM:\SOFTWARE\Policies\Google\Chrome' -ErrorAction SilentlyContinue
$token = $null
if ($chromeKey -and ($chromeKey.PSObject.Properties.Name -contains 'CloudManagementEnrollmentToken')) {
$token = $chromeKey.CloudManagementEnrollmentToken
}
if ($token) { Add-Line (" CloudManagementEnrollmentToken present: {0}" -f $token) 'Yellow' }
else { Add-Line " (no cloud enrollment token)" }
}
catch {
Add-Line (" ERROR in section 2: {0}" -f $_.Exception.Message) 'Red'
}
Add-Line ""
# ---------------------------------------------------------------------------
# 3. Real device management (Azure AD join / MDM)
# ---------------------------------------------------------------------------
$step++; Write-Progress -Activity "Diagnosing" -Status "Device enrollment" -PercentComplete (($step / $totalSteps) * 100)
Add-Line "===== 3. DEVICE ENROLLMENT (real management vs leftover) =====" 'Cyan'
try {
$ds = & dsregcmd /status 2>$null | Select-String 'AzureAdJoined|DomainJoined|WorkplaceJoined|TenantName|MdmUrl'
if ($ds) { foreach ($line in $ds) { Add-Line (" {0}" -f $line.ToString().Trim()) } }
else { Add-Line " (dsregcmd returned no join/MDM lines)" }
}
catch {
Add-Line (" ERROR running dsregcmd: {0}" -f $_.Exception.Message) 'Red'
}
Add-Line ""
# ---------------------------------------------------------------------------
# 4. Scheduled tasks that launch scripts/consoles, with missing-target flag
# ---------------------------------------------------------------------------
$step++; Write-Progress -Activity "Diagnosing" -Status "Scheduled tasks" -PercentComplete (($step / $totalSteps) * 100)
Add-Line "===== 4. SCHEDULED TASKS LAUNCHING SCRIPTS / CONSOLES =====" 'Cyan'
$missingTargets = @()
try {
$allTasks = Get-ScheduledTask -ErrorAction SilentlyContinue
foreach ($t in $allTasks) {
$exec = ($t.Actions | ForEach-Object { $_.Execute }) -join ' '
$argStr = ($t.Actions | ForEach-Object { $_.Arguments }) -join ' '
$full = ("{0} {1}" -f $exec, $argStr).Trim()
if ($full -match 'cmd|\.bat|\.cmd|powershell|pwsh|\.ps1|wscript|cscript') {
$file = Resolve-TaskFile $full
if ($file) { $exists = Test-Path $file } else { $exists = 'n/a' }
$color = $null
$flag = ''
if ($exists -eq $false) {
$color = 'Red'
$flag = ' <-- TARGET MISSING (likely restart-popup source)'
$missingTargets += $t.TaskName
}
Add-Line (" {0} [{1}] user={2} state={3} exists={4}{5}" -f `
$t.TaskName, $t.TaskPath, $t.Principal.UserId, $t.State, $exists, $flag) $color
if ($file) { Add-Line (" target: {0}" -f $file) }
}
}
if (-not $missingTargets) { Add-Line " (no task points at a missing script)" 'Green' }
}
catch {
Add-Line (" ERROR in section 4: {0}" -f $_.Exception.Message) 'Red'
}
Add-Line ""
# ---------------------------------------------------------------------------
# 5. Run keys (HKLM + HKCU)
# ---------------------------------------------------------------------------
$step++; Write-Progress -Activity "Diagnosing" -Status "Run keys" -PercentComplete (($step / $totalSteps) * 100)
Add-Line "===== 5. RUN KEYS =====" 'Cyan'
$runKeys = @(
'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run',
'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run',
'HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Run'
)
try {
foreach ($rk in $runKeys) {
if (Test-Path $rk) {
Add-Line ("[{0}]" -f $rk) 'Yellow'
$item = Get-ItemProperty -Path $rk -ErrorAction SilentlyContinue
$props = $item.PSObject.Properties | Where-Object { $_.Name -notlike 'PS*' }
foreach ($p in $props) { Add-Line (" {0} = {1}" -f $p.Name, $p.Value) }
}
}
}
catch {
Add-Line (" ERROR in section 5: {0}" -f $_.Exception.Message) 'Red'
}
Add-Line ""
# ---------------------------------------------------------------------------
# 6. Startup folders
# ---------------------------------------------------------------------------
$step++; Write-Progress -Activity "Diagnosing" -Status "Startup folders" -PercentComplete (($step / $totalSteps) * 100)
Add-Line "===== 6. STARTUP FOLDERS =====" 'Cyan'
$startupPaths = @(
(Join-Path $env:APPDATA 'Microsoft\Windows\Start Menu\Programs\Startup'),
'C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup'
)
try {
$found = $false
foreach ($sp in $startupPaths) {
$items = Get-ChildItem -Path $sp -ErrorAction SilentlyContinue
foreach ($i in $items) { Add-Line (" {0}" -f $i.FullName); $found = $true }
}
if (-not $found) { Add-Line " (startup folders empty)" }
}
catch {
Add-Line (" ERROR in section 6: {0}" -f $_.Exception.Message) 'Red'
}
Add-Line ""
# ---------------------------------------------------------------------------
# 7. Non-Microsoft services pointing at scripts
# ---------------------------------------------------------------------------
$step++; Write-Progress -Activity "Diagnosing" -Status "Services" -PercentComplete (($step / $totalSteps) * 100)
Add-Line "===== 7. SERVICES REFERENCING SCRIPTS =====" 'Cyan'
try {
$svc = Get-CimInstance Win32_Service -ErrorAction SilentlyContinue |
Where-Object { $_.PathName -match '\.bat|\.ps1|\.cmd|debloat|tweak' }
if ($svc) {
foreach ($s in $svc) { Add-Line (" {0} [{1}] {2}" -f $s.Name, $s.StartMode, $s.PathName) }
}
else {
Add-Line " (no service launches a script)" 'Green'
}
}
catch {
Add-Line (" ERROR in section 7: {0}" -f $_.Exception.Message) 'Red'
}
Add-Line ""
Write-Progress -Activity "Diagnosing" -Completed
# ---------------------------------------------------------------------------
# Summary
# ---------------------------------------------------------------------------
Add-Line "===== SUMMARY =====" 'Cyan'
if ($missingTargets) {
Add-Line (" {0} task(s) point at a missing script - the likely restart-popup cause:" -f $missingTargets.Count) 'Red'
foreach ($m in $missingTargets) { Add-Line (" - {0}" -f $m) 'Red' }
Add-Line " Fix: repoint the task at the real script, or remove the task if it is dead."
}
else {
Add-Line " No dead scheduled-task targets found." 'Green'
}
Add-Line ""
# ---------------------------------------------------------------------------
# Write report file + optional open
# ---------------------------------------------------------------------------
try {
$report | Set-Content -Path $ExportPath -Encoding UTF8
Add-Line ("Report written to: {0}" -f $ExportPath) 'Green'
}
catch {
Add-Line ("ERROR writing report: {0}" -f $_.Exception.Message) 'Red'
exit 1
}
if ($OpenReport) {
try { Start-Process notepad.exe -ArgumentList $ExportPath }
catch { Add-Line ("Could not open report: {0}" -f $_.Exception.Message) 'Yellow' }
}
exit 0
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.