9 sep 2013

create shortcut with powershell

 function Set-Shortcut ($linkpath , $targetPath )
{
    Write-Host $linkpath
    $strlinkpath = [String ]$linkpath
    if($targetPath -eq -$NULL)
    {
     throw ("targetpath not available" )
    }
    else
    {
        if(Test-Path $targetPath)
        {
        }
        else
        {
            throw " $targetPath does not exist"
        }
    }
    if($strlinkpath -eq $NULL)
    {     
            throw “linkpath not correct”
    }
    elseif($strlinkpath .EndsWith(".lnk" ))
    {
     $link            = ( New-Object -ComObject WScript.Shell). CreateShortcut($strlinkpath)
     $link.TargetPath = $targetPath
     ## Added after grawity's post
     $link.Save()
    }
    else
    {
        throw ("filename not available in path" )
    }

}