The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Automate cleaning up broken desktop shortcuts

Automating the cleanup of broken desktop shortcuts can streamline system maintenance and remove clutter. Below is a Windows-based PowerShell script that identifies and deletes broken .lnk (shortcut) files on the desktop.


PowerShell Script to Remove Broken Desktop Shortcuts

powershell
# Define desktop path $desktopPath = [Environment]::GetFolderPath("Desktop") # Get all .lnk files on the desktop $shortcuts = Get-ChildItem -Path $desktopPath -Filter *.lnk foreach ($shortcut in $shortcuts) { try { # Use COM object to resolve target path $wshShell = New-Object -ComObject WScript.Shell $shortcutPath = $wshShell.CreateShortcut($shortcut.FullName).TargetPath # If target path does not exist, delete the shortcut if (-not (Test-Path $shortcutPath)) { Write-Host "Removing broken shortcut: $($shortcut.Name)" Remove-Item -Path $shortcut.FullName -Force } } catch { Write-Host "Error processing $($shortcut.FullName): $_" } }

How to Use

  1. Open PowerShell as Administrator

  2. Copy and paste the script into the PowerShell window or save it as CleanBrokenShortcuts.ps1.

  3. Run the script by pressing Enter or executing:

    powershell
    .CleanBrokenShortcuts.ps1

Optional Enhancements

  • Run as a scheduled task weekly or monthly.

  • Log deleted shortcuts to a text file:

    powershell
    Add-Content "C:Logsdeleted_shortcuts.txt" "Deleted: $($shortcut.FullName)"
  • Scan all user desktops:

    powershell
    $userDesktops = Get-ChildItem "C:Users" | ForEach-Object { "$($_.FullName)Desktop" }

This script is safe, quick, and effective for routine desktop maintenance on Windows systems.

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About