Creating Symbolic Links

by Sep 25, 2013

Symbolic links work very similar to "regular" link files (*.lnk): they can point to virtually any file or folder and even UNC paths. Unlike lnk-files, to create symbolic links you need full Administrator privileges, and users cannot access the symlink properties.

Here is a function that creates symbolic links for you:

function New-SymbolicLink
{
    param
    (
        [Parameter(Mandatory=$true)]
        $OriginalPath,

        [Parameter(Mandatory=$true)]
        $MirroredPath,

        [ValidateSet('File', 'Directory')]
        $Type='File'

    )
    
    if(!([bool]((whoami /groups) -match "S-1-16-12288") ))
    {
        Write-Warning 'Must be an admin'
        break
    }
    $signature = '
        [DllImport("kernel32.dll")]
        public static extern bool CreateSymbolicLink(string lpSymlinkFileName, string lpTargetFileName, int dwFlags)
        '
    Add-Type -MemberDefinition $signature -Name Creator -Namespace SymbolicLink 

    $Flags = [Int]($Type -eq 'Directory')
    [SymbolicLink.Creator]::CreateSymbolicLink($MirroredPath, $OriginalPath,$Flags)

}

$downloads = "$env:userprofile\Downloads"
$desktop = "$env:userprofile\Desktop\MyDownloads"

New-SymbolicLink -OriginalPath $downloads -MirroredPath $desktop -Type Directory

When you run the code (with full Administrator privileges), it makes your downloads folder available right on your desktop. Right-click the symbolic link and choose Properties to compare this to "regular" *.lnk link files.

Twitter This Tip! ReTweet this Tip!