Test-PerfCounterHealth.ps1
Pre-flight check for Windows performance counter health before a SQL Server installation.

WHY
  SQL Server setup registers its own performance counters through lodctr. Before writing,
  it reads the existing counter list. If that list is inconsistent, setup fails with:

      Cannot load Counter Name data because an invalid index was read from the registry.

  and rolls back, usually after it has already been running for a while. The cause is
  normally older than the installation, most often an uncleanly removed monitoring or
  backup agent. This script finds that before you start setup.

USAGE
  Open an elevated PowerShell prompt, then:

      powershell.exe -NoProfile -ExecutionPolicy Bypass -File .\Test-PerfCounterHealth.ps1

  Exit code 0 = healthy, 1 = fix required.

WHAT IT CHECKS
  1. Counter name enumeration (the same read setup performs)
  2. Perflib index range: Last Counter must cover the highest index in the string list
  3. Orphaned providers: Performance keys pointing at a missing .ini under C:\Windows\INF
  4. WMI performance classes
  5. Pending reboot

WHAT IT DOES NOT DO
  Nothing. It is read-only and changes no registry value, service or file. If it reports
  a problem it prints the repair steps, it does not run them.

REQUIREMENTS
  Windows PowerShell 5.1 or PowerShell 7. Administrator rights.

Full article, including what the repair steps actually do and why renaming
PerfStringBackup.INI matters:
https://www.powershelldba.de/blog/articles/sql-server-setup-invalid-counter-name-index.html

(c) powershelldba.de / Uwe Janke, dtcSoftware. Provided as-is, no warranty.
Test it in a lab before you run it anywhere that matters.
