Windows – How to stop a Windows shortcut from updating its path

pathshortcutswindowswindows 10windows 7

While developing my product, my test device has several versions of the application installed at the same time. However, in order to get the absolute paths that my application is using to work, I need to switch out or rename the folders so that the version I want to test has the right path, say C:\Program Files\My Company\My App\My App.exe. I've made a shortcut that targets this path, but if this shortcut is opened when none of the versions currently have that path then the shortcut will automatically update itself, thinking the file has moved permanently. This caused silent failures until I discovered what was happening. I didn't know this was a feature of Windows shortcuts until now.

So my question is simple. Is there any way to turn this feature off? Globally is good but a per-shortcut solution would be better.

Using a batch file instead of a shortcut is one solution, but I'm wondering if there's any way to make this work while still using a shortcut.

I'm using Windows 10 Home and Windows 7 Home.

Best Answer

You can use PowerShell! This little script whacks the LNK file to produce the same effect as using the classic shortcut utility.

$linkfile = Resolve-Path $args[0]
$bytes = [IO.File]::ReadAllBytes($linkfile)
$bytes[0x16] = $bytes[0x16] -bor 0x36
[IO.File]::WriteAllBytes($linkfile, $bytes)

To use it, save that text as a .ps1 file, e.g. notrack.ps1. If you haven't already, follow the instructions in the Enabling Scripts section of the PowerShell tag wiki. Then you can run it from a PowerShell prompt:

.\notrack.ps1 C:\path\to\my\shortcut.lnk

Shortcuts that are tweaked in this way will not change when their target moves. If a shortcut like this gets broken, nothing at all will happen when you try to open it.

I gathered the binary math used in my script from this 48-page Microsoft PDF on the LNK format.

Related Question