PowerShellBeginner · Windows 10/11 or Windows Server; PowerShell 7+

Find missing and duplicate PATH entries

Inspect one PATH scope for nonexistent directories and repeated entries.

Before you begin

  • PowerShell 7 or later on Windows.
  • Local Windows management information must be accessible.
What this script changesRead-only local report. It does not start services, install updates, delete files, or change configuration. Output may contain account, hardware, path, or network information; review before sharing.

Set up your example

  1. Save the download as windows-path-audit.ps1.
  2. Open PowerShell 7 in the download folder.
  3. Run pwsh -NoProfile -File ./windows-path-audit.ps1 -Help, then inspect its parameter block.

This recipe uses your own local input or creates its own example output. Follow the setup steps below.

Download the ZIP, extract it, and open a terminal inside the extracted folder. The source file is windows-path-audit.ps1.

Run the script

Run command
pwsh -NoProfile -File ./windows-path-audit.ps1 -Scope User

If your system uses python3 instead of python, use that command for Python examples. Keep paths containing spaces in quotes. Required inputs and optional settings are described in the code and setup steps.

What to expect

A stale directory has Exists=False; a repeated location has Duplicate=True.

The complete script

windows-path-audit.ps1
#Requires -Version 7.0
[CmdletBinding()]
param([switch]$Help,[switch]$AsJson,[ValidateSet('Process','User','Machine')][string]$Scope='Process')
$ErrorActionPreference='Stop'
if ($Help) { Write-Output 'Inspect one PATH scope for nonexistent directories and repeated entries. Use -AsJson for structured output.'; exit 0 }
function Show-Report { param([object]$Data) if ($AsJson) { ConvertTo-Json -InputObject @($Data) -Depth 8 } else { $Data | Format-Table -AutoSize | Out-String -Width 240 | Write-Output } }
try {
$seen=[Collections.Generic.HashSet[string]]::new([StringComparer]::OrdinalIgnoreCase)
$rows=foreach($entry in ([Environment]::GetEnvironmentVariable('Path',$Scope) -split ';')){if(-not $entry.Trim()){continue};$expanded=[Environment]::ExpandEnvironmentVariables($entry.Trim().Trim('"'));[pscustomobject]@{Entry=$entry;Expanded=$expanded;Exists=(Test-Path -LiteralPath $expanded -PathType Container);Duplicate=(-not $seen.Add($expanded.TrimEnd('\')))}}
Show-Report $rows

} catch { Write-Error $_.Exception.Message; exit 1 }

Open source as plain text · Download source and sample files

How it works

  1. Inspect one PATH scope for nonexistent directories and repeated entries.
  2. The script queries local Windows data and formats a snapshot without changing settings.
  3. -AsJson changes presentation to JSON for comparison between runs.

Make it your own

Adjust the report

Change -Scope to Machine or Process to locate the source of the issue.

Capture JSON

Add -AsJson and redirect to a new unused filename. Shell redirection can overwrite an existing file, so choose a fresh name.

Change one setting at a time, run it again on sample data, and compare the output. The customization guide walks through this process.

If something goes wrong

A command is unavailable or access is denied.

Local Windows management information must be accessible. Use only permissions you are authorized to use; protected records may require elevation.

No rows are returned.

No matching data can be valid. Remove optional filters and confirm this Windows edition exposes the requested information.

Reference & next steps

These official references explain the underlying tools. The Scriptage example and guide are original instructional content. See verification notes for what has been checked and the limits of those checks.

Copies a template for your own notes. It does not send anything.