14 ott 2010

Fix Windows 7 Desktop Shortcuts Disappearing

If you run Windows 7 you might have noticed that desktop shortcuts disappear with no apparent reason whatsoever. Turns out that System Maintenance is responsible for that. It runs as a weekly maintenance service that affects desktop shortcuts in two ways.

It will first remove desktop shortcuts that have not been used for at least three months. System Maintenance will also remove broken shortcuts on the desktop if there are more than four of them. The problem with the latter is that Windows 7 will recognize shortcuts to disconnected network locations as broken and also shortcuts that link to shell folders.

Microsoft offers two solutions to the problem. The first advice is to keep the number of broken shortcuts below four. This is not always possible due to the problems with broken shortcuts mentioned above.

The second suggestion by Microsoft is to turn off Computer Maintenance:

  • Click Start, and then click Control Panel.
  • Under System and Security, click Find and fix problems.
  • On the left navigation pane, click Change settings.
  • Set Computer Maintenance to Off.

This does however turn off all other computer maintenance related tasks like checking the system time, disk volume errors or the disk space error reports.

All the maintenance tasks are powered by scripts that are located in c:\windows\Diagnostics\Scheduled\Maintenance\. Options are to edit the scripts directly in that directory. The permissions prohibit that the files are edited by the current user.

Fixing Windows 7 Desktop Shortcuts Disappearing

Please note: This is a hack which might have consequences on other parts of the operating system. Everything worked fine afterwards on the test system but make sure to backup the files properly before editing them

First we need to take control of the files so that we can edit them. To do that you need to take ownership first and then modify the access rights of the current user so that the files can be modified.

The first part can be automated. Just download the script at this forum and execute it. This should add a Take Ownership entry to the right-click menu in Windows Explorer.

Go to C:\Windows\diagnostics\scheduled\Maintenance in Windows Explorer after installing the script and right-click the file TS_BrokenShortcuts.ps1. Select the Take Ownership option which should transfer ownership to the logged in user.

The user still does not have permissions to save the file which means that we have to complete the next step as well before we can edit the broken shortcuts Powerscript file so that it will not delete broken shortcuts automatically.

Open an elevated command line prompt. You do that by clicking on the Start orb, All Programs, then Accessories. Right-click the Command Prompt link and select to Run As Administrator.

Now issue the following command to grant the active user full rights of the file. Make sure you replace USERNAME with the name of the active user.

icacls c:\windows\diagnostics\scheduled\maintenance\TS_BrokenShortcuts.ps1 /grant USERNAME:F

Now you can edit the file normally in a text editor like Notepad. Open it and locate the line (line 11)

[string]$list = ""

Delete everything below that is below that line up to (line 22)

return $list

The lines that are deleted are:

Get-ChildItem -Path $path -filter *.lnk | Foreach-Object {
$fullPath = ConvertTo-WQLPath $_.FullName
$wmiLinkFile = Get-WmiObject -query "SELECT Name,Target,AccessMask FROM Win32_ShortcutFile WHERE Name = '$fullPath'"

if(-not(Test-ValidLink $wmiLinkFile) -and (Test-Delete $wmiLinkFile))
{
$list = AttachTo-List $list $wmiLinkFile.Name
}
}

This basically returns a blank list to the script so that no shortcuts get deleted. I have tested it with five broken shortcuts and they have not been deleted after running system maintenance.

That fixed the broken shortcuts but we still have the issue that shortcuts that have not been used for some time are deleted by Windows 7 as well.

We have to (almost) perform the same operation here. Locate the file TS_UnusedDesktopIcons.ps1 in the same directory. Take ownership of it and grant the active user access rights.

Edit the file afterwards and locate the line (24)

[string]$list = ""

Delete everything up to line (35)

return $list

so that the following lines get deleted

Get-ChildItem -Path $path -filter *.lnk | Foreach-Object {
$fullPath = ConvertTo-WQLPath $_.FullName
$wmiLinkFile = Get-WmiObject -query "SELECT Name,Target,AccessMask FROM Win32_ShortcutFile WHERE Name = '$fullPath'"
$lastAccessTime = Get-LastAccessTime $_.FullName

if((Test-ValidLink $wmiLinkFile) -and (Test-Delete $wmiLinkFile) -and (Test-FileShortcut $wmiLinkFile) -and (Test-Unused $lastAccessTime $threshold))
{
$list = AttachTo-List $list $wmiLinkFile.Name
}
}

Please note that I did not test the second script but it should work just as fine.

This should resolve all issues that users have with removed shortcuts on the Windows 7 desktop.

source: http://www.ghacks.net/2010/03/30/fix-windows-7-desktop-shortcuts-disappearing/

Nessun commento:

Posta un commento