I've recently been digging into the SHellLink specification, which defines the structures that make up a shortcut (.lnk) file. There is a way to modify shortcuts where the target is a file folder, e.g. This PC > Docuements, so that they always display the true file system path.
All .lnk files have a set of flags that specify the presence or absence of various structures as well as govern link resolution. Among these flags, listed in section 2.1.1 of the documentation specified above,is:
| Name |
Description |
| NoPidlAlias |
The file system location is represented in the shell namespace when the path to an item is parsed into an IDList. |
Neither of the shortcut com objects expose direct manipulation of the flags, so we have resort to editing the binary .lnk file. Fortunately, this is rather trivial as we only want to flip a single bit and the associated byte is at a fixed offset. I've wrapped this up in a PowerShell function that can modify on or multiple shortcuts at once:
Function Set-NoPidlAlias {
Param(
[Alias('FullName')]
[Parameter(Mandatory, Position=0, ValueFromPipeline, ValueFromPipelineByPropertyName)]
[ValidateScript({(Test-Path $_ -PathType Leaf) -and ($_ -match '\.lnk$')})]
[String]$Path,
[Switch]$Undo
)
Process {
ForEach ( $p in @(Resolve-Path $Path).Path)
{
[Byte[]]$LinkBytes = [IO.File]::ReadAllBytes($p)
If (! $Undo) {
$LinkBytes[0x15] = $LinkBytes[0x15] -bor 0x80
} Else {
$LinkBytes[0x15] = $LinkBytes[0x15] -band (-bnot 0x80)
}
[IO.File]::WriteALlBytes($p, $LinkBytes)
}}}
Examples:
> Set-NoPidlAlias c:\users\me\shortcuts\MtySHortcut.lnk
> Set-NoPidlAlias myShortcut.lnk ### link in current directory
> Set-NoPidlAlias *.lnk ### all shortucuts in current directory
> gci *.lnk -Recurse | Set-NoPidlAlias ### all links in current directory and sub-directories.
After setting (or clearing) the flag, the target path needs to be "touched" by:
- Display the file's
Properties dialog.
- Delete or add a character to the path and then restore the text to the correct path --- this tricks the shell into thinking the link has been edited and the
Apply button becomes active.
- Click
OK or Apply and close.