Windows Short-cut vs Symbolic Link vs Junction vs Hard Link

Although they behave similarly, there are fundamental differences between a short-cut (a Microsoft’s implementation for Windows GUI), a directory symbolic link (POSIX standardized), a junction, and a hard link.

“Shortcuts are treated like ordinary files by the file system and by software programs that are not aware of them. Only software programs that understand shortcuts (such as the Windows shell and file browsers) treat them as references to other files. Symbolic links are automatically resolved by the file system. Any software program, upon accessing a symbolic link, will see the target instead, whether the program is aware of symbolic links or not.” (Source: wikipedia).

Moreover, symbolic links are processed at the client, and junctions as well as hard links are processed at the server. This means that symbolic links can reference network shares and mapped drives, whereas junctions and hard links cannot. It is notable that Microsoft’s implementation of Symbolic links are slightly different than Linux in part where indications of files and directories must be specified in Windows.

To distinguish the two latter types, “a junction (also called a soft link) differs from a hard link in that the storage objects it references are separate directories, and a junction can link directories located on different local volumes on the same computer. Otherwise, junctions operate identically to hard links.” (Source: Microsoft)

Examples to convey these concepts:
Hard links

– Allowed:
— C:\folder1\nottestfile.txt linked to C:\folder2\testfile.txt
– Not allowed:
— C:\folder1 linked to C:\folder2
— C:\folder1 linked to D:\folder1

Junctions

– Allowed:
— C:\folder1 linked to C:\folder2
— C:\folder1 linked to D:\folder1
– Not allowed:
— C:\folder1\nottestfile.txt linked to C:\folder2\testfile.txt
— C:\folder1 linked to \\SMB\SHARE or mapped drive Z:\

Symbolic Links

– Allowed:
— C:\folder1 linked to C:\folder2
— C:\folder1 linked to D:\folder1
— C:\folder1 linked to \\SMB\SHARE or mapped drive Z:\
– Not allowed:
— \\NETWORK\SHARE linked to C:\Folder1. Where there’s a shortcut or symlink from C:\Folder1\testfile.txt to D:\testfile.txt, users cannot access that \\NETWORK\SHARE\testfile.txt shortcut or symlink

Shortcuts

– Allowed:
— Same as symbolic links
– Not allowed:
— Same as symlinks
— Point to a destination via an relative path (e.g. ./Folder1)

Here’s a practical application of moving files from one disk to another by creating a junction from original location to its new destination:

Script to Move Storage

# Identify the locations
$originalFolder='C:\folderName'
$moveToParentFolder='D:\'

# Execute under a local System Adminstrator context to move a folder
Move-Item -Path $originalFolder $moveToParentFolder -Force

# Create a junction from original location to new location
$newLocation=Join-Path -Path $moveToParentFolder -ChildPath $(split-path $originalfolder -leaf)
$null=cmd /c mklink /J $originalFolder $newLocation # This cmdlet works on all Windows versions

DOS mklink vs PowerShell version 5.0 commands

# +-----------------------+-----------------------------------------------------------+
# | mklink syntax         | Powershell equivalent                                     |
# +-----------------------+-----------------------------------------------------------+
# | mklink Link Target    | New-Item -ItemType SymbolicLink -Name Link -Target Target |
# | mklink /D Link Target | New-Item -ItemType SymbolicLink -Name Link -Target Target |
# | mklink /H Link Target | New-Item -ItemType HardLink -Name Link -Target Target     |
# | mklink /J Link Target | New-Item -ItemType Junction -Name Link -Target Target     |
# +-----------------------+-----------------------------------------------------------+

Leave a Reply

Your email address will not be published. Required fields are marked *