Skip to content
CeeWeasel edited this page Jan 24, 2014 · 3 revisions
Function Make-Link
{
<#
    .SYNOPSIS
        A PowerShell version of mklink

    .DESCRIPTION
        This is used to create file symbolic links, directory symbolic links, hard links, or directory junctions.

    .PARAMETER Link
        Specifies the new symbolic link name.

    .PARAMETER Target
        Specifies the path (relative or absolute) that the new link refers to.

    .PARAMETER Backup
        If files are found at the target location, they are moved to this location.
        If this is not specified, any files at the target location will be lost.

    .PARAMETER Directory
        Creates a directory symbolic link.

    .PARAMETER File
        Creates a file symbolic link.

    .PARAMETER Hard
        Creates a hard link instead of a symbolic link.

    .PARAMETER Junction
        Creates a Directory Junction.

    .EXAMPLE

        -------------------------- EXAMPLE 1 --------------------------

        PS C:\>Make-Link -Link Documents -Target 'C:\Users\JohnSmith\My Documents' -Directory

        This command creates a directory symbolic link at the current location called Documents that links to
        'C:\Users\JohnSmith\My Documents'

        -------------------------- EXAMPLE 2 --------------------------

        PS C:\>Make-Link C:\MyPictures C:\SharedPictures -Directory -Backup C:\MyPicturesBackup

        This command creates a directory symbolic link at 'c:\' named 'MyPictures'.  The link points to 'C:\SharedPictures'.
        If a folder exists at 'C:\MyPictures' the contents are backed up to 'C:\MyPicturesBackup'

    .NOTES
        FunctionName : Make-Link
        Created by   : CeeWeasel
        Date Coded   : 01/22/2014 14:57:48

    .LINK
         https://github.com/CeeWeasel/PowerShell/wiki/Make-Link
#>
[CmdletBinding()]
Param
(
    [Parameter(
        Mandatory=$true,
        Position=1
    )]
    [string]$Link,

    [Parameter(
        Mandatory=$true,
        Position=2
    )]
    [string]$Target,

    [Alias("b")]
    [Parameter(
        Mandatory=$false
    )]
    [string]$Backup,

    [Alias("d")]
    [Parameter(
        ParameterSetName="Directory",
        Mandatory=$false
    )]
    [switch]$Directory,

    [Alias("f")]
    [Parameter(
        ParameterSetName="File",
        Mandatory=$false
    )]
    [switch]$File,

    [Alias("h")]
    [Parameter(
        ParameterSetName="Hard",
        Mandatory=$false
    )]
    [switch]$Hard,

    [Alias("j")]
    [Parameter(
        ParameterSetName="Junction",
        Mandatory=$false
    )]
    [switch]$Junction

)
    Begin
    {
        if ( $Directory ) { $CMDFlag = "/d" }
        if ( $File ) { $CMDFlag = "" }
        if ( $Hard ) { $CMDFlag = "/h" }
        if ( $Junction ) { $CMDFlag = "/j" }
    }

    Process
    {
        if ( ! ( Test-Path $Target ) ) { Return "The target directory does not exist" }

        if ( $Backup )
        {
            Write-Verbose "Starting backup process"
            if ( ! ( Test-Path $Backup ) )
            {
                Write-Verbose "Specified backup directory ( $Backup ) not found.  Trying to create."
                try { New-Item -path $Backup -ItemType Directory }
                catch
                {
                    $Result = "Error creating backup directory"
                    $Message = $error[0]
                    Write-Verbose "$Message"
                    Return $Result
                }
                Write-Verbose "Successfully created backup directory"
            }

            $Message = "Backing up files at " + $Link + " to " + $Backup
            Write-Verbose $Message
            try { Copy-Item $Link -Destination $Backup -Recurse }
            catch
            {
                $Result = "Error backing up files"
                $Message = $error[0]
                Write-Verbose "$Message"
                Return $Result
            }
        }

        if ( Test-Path $Link )
        {
            $CMDInstructions = "rmdir " + """ + $Link + """ + " /s /q"
            Write-Verbose "A directory exists at $Link.  Removing directory by running $CMDInstructions"
            $Removal = CMD.exe /c $CMDInstructions
            Write-Verbose $Removal
        }

        $CMDInstructions = "mklink " + $CMDFlag + " " + """$Link"" ""$Target"""
        Write-Verbose "Attempting to make the link using the command $CMDInstructions"
        $Result = CMD.exe /c $CMDInstructions
    }

    End { Return $Result }
}

Clone this wiki locally