Quantcast
Channel: Microsoft – JeffOps
Viewing all articles
Browse latest Browse all 120

Script to remove invalid shortcuts from start menu

$
0
0

Last week I was asked to write a script that could be executed on desktops in the user context as part of a pre-migration script.
The goal of the script was to check for, and remove, invalid shortcuts from the Start Menu / Start Screen.
Luckily I did this once or twice before, so I dove in my repository and shared it.
So, here is the script… Hope you find it useful :-)

Function Remove-InvalidStartMenuItem {
    $WshShell = New-Object -comObject WScript.Shell
    $Files = Get-ChildItem -Path "C:\Users\$env:USERNAME\AppData\Roaming\Microsoft\Windows\Start Menu\Programs" -Filter *.lnk
    foreach ($File in $Files) {
        $FilePath = $File.FullName
        $Shortcut = $WshShell.CreateShortcut($FilePath)
        $Target = $Shortcut.TargetPath
        if (Test-Path -Path $Target) {
            Write-Output "Valid: $($File.BaseName)"
        } else {
            Write-Output "Invalid: $($File.BaseName) removed."
            try {
              Remove-Item -Path $LnkFilePath
              Write-Output "Removed: $($File.BaseName) removed."
            } catch {
              Write-Output "ERROR: $($File.BaseName) not removed."
            }
        }
    }
}

Viewing all articles
Browse latest Browse all 120

Trending Articles