Helpers/Profile/Set-ProfileFolderLink.ps1


function Set-ProfileFolderLink
{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $true)]
        [ValidateScript({ Test-Path -Path $_ })]
        [System.String]
        $Path
    )

    $ProfileFolder = [System.Environment]::GetFolderPath('MyDocuments') + '\WindowsPowerShell'

    if ((Test-Path -Path $ProfileFolder))
    {
        # The profile folder or a symlink does already exist. Therefore we
        # will verify the symlink.
        $ProfileFolderItem = Get-Item -Path $ProfileFolder

        # Verify if the profile folder is a directory symlink and points to the
        # specified target.
        if ($ProfileFolderItem.PSIsContainer -eq $false -or $ProfileFolderItem.LinkType -ne 'SymbolicLink' -or $ProfileFolderItem.Target[0] -ne $Path)
        {
            throw "Item '$ProfileFolder' already exist, but it is not a directory symbolic link to '$Path'!"
        }
    }
    else
    {
        # The profile folder nor a symlink does exist. Therefore we will create
        # a new symlink.
        try
        {
            New-Item -ItemType SymbolicLink -Path $ProfileFolder -Value $Path -ErrorAction Stop | Out-Null
        }
        catch
        {
            throw "Unable to link '$ProfileFolder' to '$Path': $_"
        }
    }
}