PowerShell How-To

How To Manage Shortcuts with PowerShell

Save time and look like a hero by easily fixing broken shortcuts.

In a business environment, it is common to find end-user Windows clients that have had dozens of applications installed over the years, with shortcuts created over multiple user profiles. Many of these shortcuts were created by the applications themselves during installation. On top of that, you also have users manually creating shortcuts to these applications (in who knows how many different places). Not only will these Windows clients have numerous LNK shortcuts, they'll also have various URL shortcuts -- either placed directly on users' desktops or bookmarked as Internet Explorer favorites.

So what happens when an application is upgraded or a Web site URL changes? The shortcuts aren't automatically updated to reflect this, which leads to confusion among the users, resulting in an increase in help desk calls. Traditionally, the solution has been to wait for a help desk call, and then walk the user through the process of removing the old shortcut and creating a new one. This wastes a lot of time for both the user and the help desk operator and prevents the user from performing their primary responsibilities.

By utilizing a set of PowerShell functions, you can now find all of these shortcuts in any folder on a client machine. Not only can you see the shortcuts, you can remove them if the application has been uninstalled. You can also update the shortcut to reflect a new path. All of this can be accomplished with just a couple of lines in a PowerShell script. Here's how.

I've created a PowerShell module that you can import. It contains a script that manages all of these shortcuts for you. Let's look at an example showing how it works. We'll do this by finding and changing a target path for all shortcuts.

Select a client that you know has one or more shortcuts you'd like to change. Find the current target path. In our example, we want to change the target path for the Notepad++ application.

[Click on image for larger view.] Figure 1.

Next, download the ShortcutManagement PowerShell module and import the module:

Import-Module  C:\ShortcutManagement.psm1

You can use the Get-Shortcut cmdlet to discover all Notepad++ shortcuts in all user profiles based on the target path discovered in Step 1.

Get-Shortcut  –TargetPath 'C:\Program Files\Notepad++\notepad++.exe
[Click on image for larger view.]  Figure 2.

As you can see, we have two shortcuts in two Start menu folders. Let's change that shortcut path to C:\Program Files\Notepad++v2\notepad++.exe.

[Click on image for larger view.]  Figure 3.

That's it! Both of the shortcuts are now pointing to the new path. Under the covers, both the Get-Shortcut and Set-Shortcut cmdlets are using the Wscript.Shell COM object. This object has a CreateShortcut() method that enables it to interrogate a LNK or URL file and see properties, such as the target path and description. The CreateShortcut() method is the part that does all the heavy lifting.  The rest of the module is simply the glue that creates the cmdlets. It enables pipeline use -- as you saw in the example -- which enables you to process more than one shortcut at a time.

It's a good habit to use the ShortcutManagement PowerShell module every time you change software on multiple computers. It will ultimately eliminate many help desk calls, keep your users happy and (hopefully) impress your boss with your attention to detail!

About the Author

Adam Bertram is a 20-year veteran of IT. He's an automation engineer, blogger, consultant, freelance writer, Pluralsight course author and content marketing advisor to multiple technology companies. Adam also founded the popular TechSnips e-learning platform. He mainly focuses on DevOps, system management and automation technologies, as well as various cloud platforms mostly in the Microsoft space. He is a Microsoft Cloud and Datacenter Management MVP who absorbs knowledge from the IT field and explains it in an easy-to-understand fashion. Catch up on Adam's articles at adamtheautomator.com, connect on LinkedIn or follow him on Twitter at @adbertram or the TechSnips Twitter account @techsnips_io.


comments powered by Disqus
Most   Popular