How to clean up those Desktop Shortcuts…

You’ve probably seen and read that we now can deploy Edge without creating yet another desktop shortcut. Which is nice. Both Michael Niehaus and Per Larsen have blogged about how to do this (links at the end). And as Michael mentioned in his blog, only if Teams did the same …

Anyhow, so what if you’ve already deployed a bunch of applications that created shortcuts on your desktop?

If you also have enabled ‘Enterprise State Roaming’ and do a reset of your device, it’s even more fun! Once you’ve logged in again and have got a fresh new version of Edge and Teams, it’ll sync your profile and suddenly you have two (or maybe a lot more) Edge/Teams shortcut(s) on your desktop.

PowerShell to the rescue ?

The script has two parameters: Scope and Filter. The scope can either be ‘CurrentProfile’ or ‘AllProfiles’ depending on your needs. Both are required. The filter can be “” (double quotation marks) to remove all (!) shortcuts on the desktop, or any part of the shortcut name you want to remove. Examples: “oo” (without the quotation marks) would remove ‘Facebook’ but not ‘OneNote’. “Microsoft” would remove every shortcut with that in its name like ‘Microsoft Edge’ or ‘Microsoft Teams’ or ‘Something from Microsoft’.

Further, I learned that a user might have multiple desktops. Yes, you read correctly. If the user has a policy for “known folder move” it’ll move the desktop to the ‘OneDrive\Desktop’. The user might also have OneDrive for Business which again could make the user have yet another desktop – ‘OneDrive Corp Name\Desktop’

On top of that you have the public profile which desktop folder is labeled ‘Public Desktop’ instead of the normal ‘Desktop’…

If you use the ‘AllProfiles’ switch the code looks like this:

[1] First we’ll get all the profiles. [2] Then loop through everyone. [3] If it’s not the labeled ‘Public’ then [4] find out how many desktop folders the user has. For each desktop, figure out what the path is for that one specific [5]. Lastly, lets send it off to the ‘Remove-Shortcut’ function with that path [6].

Note! Both the ‘Public’ and the ‘CurrentProfile’ code have the same logic. Just in case.

function Remove-Shortcut

[1] It writes to the ‘Event Viewer’ in the ‘Application’ log user source name ‘Remove-DesktopShortcut’ for easy filtering. It’ll give you event ID 7 for Informational, 14 for Warning and 31 for Error. Don’t ask. You can change it to whatever you want.

[2] Then collecting all files on the current desktop that matches ‘.lnk’. Yeah, I know, I could potentially have a file labled ‘My.lnk.Exe’ but we’ll take care of that in our ‘IF’ statement [3] where we check for the file extension name. So, if the file is labled ‘My.lnk.exe’ it wouldn’t get past this check as the file object extension name is .exe. It’ll of course also verify that the name matches the filter you specified.

If all that is okay, it’s time for deletion! [4]

Again, as I have mentioned in a previous post. Due to limitations in MEM Intune when adding a PowerShell script, we need to call the function from inside the script with the desired parameters. Example:

Have fun!

Links