Functions/New-Shortcut.ps1


function New-Shortcut {
    [CmdletBinding()]
    param (
        [parameter(mandatory = $true)][string]$ShortcutLocation,
        [parameter(mandatory = $true)][string]$ShortcutTarget,
        [string]$Arguments,
        [string]$IconLocation
    )

    Write-Verbose "Creating shortcut with parameters:"
    Write-Verbose ($PSCmdlet.MyInvocation.BoundParameters | Out-String)

    $shortcutTargetParent = Split-Path $ShortcutTarget -Parent

    $WshShell = New-Object -ComObject WScript.Shell

    $Shortcut = $WshShell.CreateShortcut($ShortcutLocation)
    if ($Arguments) {
        $Shortcut.Arguments = $Arguments
    }
    $Shortcut.TargetPath = $ShortcutTarget
    if ($shortcutTargetParent) {
        $Shortcut.WorkingDirectory = $shortcutTargetParent
    }
    if ($IconLocation) {
        $Shortcut.IconLocation = $IconLocation
    }

    if ($Shortcut.Save()) {

        Write-Verbose "Shortcut created with the following parameters:"
        Write-Verbose ($Shortcut | Out-String)

    }
}