[CmdletBinding()] param( [string]$Csv ) $ErrorActionPreference = 'Stop' $machineRaw = (Get-ItemProperty -LiteralPath 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment' -Name Path -ErrorAction SilentlyContinue).Path $userRaw = (Get-ItemProperty -LiteralPath 'HKCU:\Environment' -Name Path -ErrorAction SilentlyContinue).Path $sources = [ordered]@{ Machine = $machineRaw User = $userRaw } $rows = New-Object System.Collections.Generic.List[object] $seen = @{} foreach ($scope in $sources.Keys) { $raw = $sources[$scope] if (-not $raw) { continue } $entries = @($raw -split ';') for ($index = 0; $index -lt $entries.Count; $index++) { $entry = $entries[$index].Trim() $issues = New-Object System.Collections.Generic.List[string] if ($entry -eq '') { $issues.Add('пустой элемент') $rows.Add([pscustomobject]@{ Scope = $scope; Index = $index; Entry = ''; Expanded = ''; Exists = $false; Duplicate = $false; Issues = ($issues -join '; ') }) continue } $clean = $entry.Trim('"') $expanded = [Environment]::ExpandEnvironmentVariables($clean) $exists = Test-Path -LiteralPath $expanded -PathType Container $key = $expanded.TrimEnd('\').ToLowerInvariant() if (-not $key) { $key = $entry.ToLowerInvariant() } $duplicate = $seen.ContainsKey($key) if (-not $duplicate) { $seen[$key] = $true } if (-not $exists) { $issues.Add('нет каталога') } if ($duplicate) { $issues.Add('дубликат') } if ($entry -match '^".*"$') { $issues.Add('кавычки') } if ($clean -match '\\$') { $issues.Add('лишний обратный слеш') } $rows.Add([pscustomobject]@{ Scope = $scope Index = $index Entry = $entry Expanded = $expanded Exists = $exists Duplicate = $duplicate Issues = ($issues -join '; ') }) } } $rows = @($rows) Write-Host ("Элементов PATH: {0}, с замечаниями: {1}" -f $rows.Count, @($rows | Where-Object { $_.Issues }).Count) $rows | Format-Table -AutoSize -Wrap if ($Csv) { $parent = Split-Path -Parent $Csv if ($parent -and -not (Test-Path -LiteralPath $parent)) { New-Item -ItemType Directory -Path $parent | Out-Null } $rows | Export-Csv -LiteralPath $Csv -NoTypeInformation -Encoding UTF8 Write-Host "Отчёт сохранён: $Csv" } Write-Host 'PATH не изменялся. Исправляйте найденные элементы вручную через свойства системы.'