[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'High')] param( [string]$Path = $env:USERPROFILE, [switch]$Apply ) $ErrorActionPreference = 'Stop' if (-not (Test-Path -LiteralPath $Path -PathType Container)) { throw "Каталог не найден: $Path" } $resolved = (Resolve-Path -LiteralPath $Path).ProviderPath.TrimEnd('\') if ($resolved -eq ([System.IO.Path]::GetPathRoot($resolved)).TrimEnd('\')) { throw 'Нельзя использовать корень диска.' } if ($resolved.StartsWith($env:SystemRoot, [System.StringComparison]::OrdinalIgnoreCase)) { throw 'Нельзя использовать системный каталог Windows.' } $shell = New-Object -ComObject WScript.Shell $shortcuts = @( Get-ChildItem -LiteralPath $resolved -Filter '*.lnk' -File -Recurse -Force -ErrorAction SilentlyContinue ) $broken = @() foreach ($shortcut in $shortcuts) { try { $target = $shell.CreateShortcut($shortcut.FullName).TargetPath if ([string]::IsNullOrWhiteSpace($target)) { continue } if (-not (Test-Path -LiteralPath $target)) { $broken += [pscustomobject]@{ Shortcut = $shortcut.FullName Target = $target Modified = $shortcut.LastWriteTime } } } catch { Write-Verbose "Пропуск $($shortcut.FullName): $($_.Exception.Message)" } } if ($broken.Count -eq 0) { Write-Host 'Битых ярлыков не найдено.' return } Write-Host ("Битых ярлыков: {0}" -f $broken.Count) $broken | Format-Table -AutoSize if (-not $Apply) { Write-Host 'Предпросмотр. Для удаления битых ярлыков добавьте -Apply.' return } $removed = 0 $failed = 0 foreach ($item in $broken) { if ($PSCmdlet.ShouldProcess($item.Shortcut, 'Удалить битый ярлык')) { try { Remove-Item -LiteralPath $item.Shortcut -Force -ErrorAction Stop $removed++ } catch { $failed++ Write-Warning "Не удалось удалить $($item.Shortcut): $($_.Exception.Message)" } } } Write-Host "Удалено: $removed, ошибок: $failed"