How to Change Shortcut (.lnk) Target in Bulk Using Script

Many users have shortcuts pointing to various network shares from other systems or your domain’s storage server. If your company migrates the server and changed the computer name, share name or the folder path, you need to manually update the shortcut targets to point to the right server. Although the folder structure may remain the same, the server name usually changes.

For shortcuts pointing to a file or folder in the local machine, Windows updates the shortcuts automatically (using “Distributed Link Tracking” service) when the target is moved or renamed. But, in the case of shortcuts pointing to network locations, they have to be done manually or using a program/script.

Updating the paths manually is a tiring task especially if you have hundreds of shortcuts. If you’re looking for information on how to bulk update shortcut targets, this article has a solution.

Change Shortcut (.lnk) Targets in Bulk

Here is a PowerShell script that updates the shortcut target for .lnk files in a specified folder automatically. This script automatically updates shortcuts that contain the old server name or path, replacing them with the new name or path.



$oldPrefix = "\\MEDIA"
$newPrefix = "\\MEDIA-PC"
$searchPath = "E:\Shortcuts"

$shell = new-object -com wscript.shell
write-host "Updating shortcut target" -foregroundcolor red -backgroundcolor black

dir $searchPath -filter *.lnk -recurse | foreach {
$lnk = $shell.createShortcut( $_.fullname )
$oldPath= $lnk.targetPath
$lnkRegex = "^" + [regex]::escape( $oldPrefix )

if ( $oldPath -match $lnkRegex ) {
$newPath = $oldPath -replace $lnkRegex, $newPrefix

write-host "Found: " + $_.fullname -foregroundcolor yellow -backgroundcolor black
write-host " Replace: " + $oldPath
write-host " With: " + $newPath
$lnk.targetPath = $newPath
$lnk.Save()
}
}
  1. Copy the above code to Notepad, and save the file as lnk_change.ps1
  2. Make sure you input the old and new paths in lines 1 & 2 above.
  3. In line #3, specify the folder which stores the shortcuts for which you want to update the path.
  4. From the Run dialog, execute the following command:
    powershell -noexit -ExecutionPolicy ByPass -File D:\Scripts\lnk_change.ps1

    change shortcut .lnk target in bulk

That’s it. The shortcut targets are now updated from \\MEDIA to \\MEDIA-PC.

  • You can also use the full path to the resource, such as \\MEDIA\Shared and \\MEDIA-PC\SharedFolder as the “old” and “new” paths respectively.
  • In case the server name is the same but the folder path has changed, you must input the full paths in line numbers 1 & 2 respectively — e.g., \\MEDIA\Shared to \\MEDIA\SharedFolder

Hope that helps!


One small request: If you liked this post, please share this?

One "tiny" share from you would seriously help a lot with the growth of this blog. Some great suggestions:
  • Pin it!
  • Share it to your favorite blog + Facebook, Reddit
  • Tweet it!
So thank you so much for your support. It won't take more than 10 seconds of your time. The share buttons are right below. :)

Ramesh Srinivasan is passionate about Microsoft technologies and he has been a consecutive ten-time recipient of the Microsoft Most Valuable Professional award in the Windows Shell/Desktop Experience category, from 2003 to 2012. He loves to troubleshoot and write about Windows. Ramesh founded Winhelponline.com in 2005.

7 thoughts on “How to Change Shortcut (.lnk) Target in Bulk Using Script”

  1. Thanks, i was looking for something like this after merging two folders into one and all my shortcuts pointed to the separate folders but now they’re all correct thanks to your script, you saved me loads of time by not having to go through each shortcuts properties and changing them by hand.

    Reply
  2. Thanks, this works for the exe. But it doesn’t change the parameters following the exe in the target field of the shortcut which also need the same text replacement made to them. Is there a way to modify the script to fix those too? (I don’t know how to program regex or powershell.)

    Reply
  3. Dave is correct! It’s very useful, but it does NOT include any of the arguments within the Target field, which unfortunately make it unreliable unless there is a way to include all of the arguments as well.

    Reply
  4. I have discovered just now that the way to do this is by adding into the script the field:

    $lnk.Arguments

    which contains any arguments that come at the end of the Target. 🙂

    Reply
    • Could you help me about this issue Mark. Where should add “$lnk.Arguments” . Could you give me the exact example for this. Thanks.

  5. I have a bunch of shortcut .lnk files that have a target like \\unc1 and they also have a Start In of \\unc1. When I run the script to update the target to \\unc2, the Start In still remains \\unc1.

    Any way to fix that?

    Reply

Leave a Comment