Force the icons on the desktop to refresh after deleting items, or stop an item from being added in the first place

Question:

I have created a powershell script that listens for files to be created on the desktop. The file is immediately deleted if it meets certain criteria. I used Remove-Item $path where $path is the path to the file I want to delete. The problem is that windows still adds, and continues to show the item on the desktop. The file is definitely not there, since attempting to manipulate it will result in a ‘Could not find this item’, or ‘File does not exist’ error. Manually refreshing the desktop via ‘Right Click => Refresh’ will cause the item to be removed.

Is there a way to force the desktop to refresh after deleting an item on it? Otherwise, is there an alternate method to delete the file to prevent it being added in the first place?

Answer:

ou can use the SHChangeNotify from Shell32.dll

You’ve got a function in former PowerShell.com, no longer available

Source:

Force the icons on the desktop to refresh after deleting items, or stop an item from being added in the first place by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply