Seems the Windows scripting host can only create URL shortcuts with a TargetPath.
http://msdn.microsoft.com/en-us/library/265a4017(v=vs.84).aspx
I removed the Mandatory Parameter and added code to handle the different cases.
Function New-Shortcut {
<#
.SYNOPSIS
Creates a new shortcut .lnk or .url file, which can be used for example on the start menu.
.DESCRIPTION
Creates a new shortcut .lnk or .url file, with configurable options.
.EXAMPLE
New-Shortcut -Path "$envProgramData\Microsoft\Windows\Start Menu\My Shortcut.lnk" -TargetPath "$envWinDir\system32\notepad.exe" -IconLocation "$envWinDir\system32\notepad.exe" -Description "Notepad" -WorkingDirectory "$envHomeDrive\$envHomePath"
.PARAMETER Path
Path to save the shortcut
.PARAMETER TargetPath
Target path or URL that the shortcut launches
.PARAMETER Arguments
Arguments to be passed to the target path
.PARAMETER IconLocation
Location of the icon used for the shortcut
.PARAMETER Description
Description of the shortcut
.PARAMETER WorkingDirectory
Working Directory to be used for the target path
.PARAMETER ContinueOnError
Continue if an error is encountered
.NOTES
.LINK
Http://psappdeploytoolkit.codeplex.com
#>
Param (
[Parameter(Mandatory = $true)]
[string] $Path,
[Parameter(Mandatory = $true)]
[string] $TargetPath,
[string] $Arguments,
[string] $IconLocation,
[string] $Description,
[string] $WorkingDirectory,
[boolean] $ContinueOnError = $true
)
$PathDirectory = ([System.IO.FileInfo]$Path).DirectoryName
Try {
If (!(Test-Path -Path $PathDirectory)) {
Write-Log "Creating shortcut directory..."
New-Item -ItemType Directory -Path $PathDirectory -ErrorAction SilentlyContinue -Force | Out-Null
}
}
Catch [Exception] {
If ($ContinueOnError -eq $true) {
Write-Log $("Failed to create shortcut directory [$PathDirectory]: " + $_.Exception.Message)
}
Else {
Throw $("Failed to create shortcut directory [$PathDirectory]: " + $_.Exception.Message)
}
}
Write-Log "Creating shortcut [$path]..."
Try {
If (($path).Endswith(".url")) {
$shortcut = $shell.CreateShortcut($path)
$shortcut.TargetPath = $targetPath
$shortcut.Save()
}
If (($path).Endswith(".lnk")) {
$shortcut = $shell.CreateShortcut($path)
$shortcut.TargetPath = $targetPath
$shortcut.Arguments = $arguments
$shortcut.IconLocation = $iconLocation
$shortcut.Description = $description
$shortcut.WorkingDirectory = $workingDirectory
$shortcut.Save()
}
}
Catch [Exception] {
If ($ContinueOnError -eq $true) {
Write-Log $("Failed to create shortcut [$path]: " + $_.Exception.Message)
}
Else {
Throw $("Failed to create shortcut [$path]: " + $_.Exception.Message)
}
}
}