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 🙂
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
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 -Recurse 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 $FilePath Write-Output "Removed: $($File.BaseName) removed." } catch { Write-Output "ERROR: $($File.BaseName) not removed." } } } } |
I’d recommend adding a -recurse switch on your get-childitem cmd otherwise you’ll be getting a short list 🙂
So what does this script achieve ?
I thought the title of the post was pretty self-explanatory?
Replace $LnkFilePath with $FilePath then the script works. Also agree with Travis.