I want to use powershell to modify the TargetPath of a shortcut to open a website I found the below script that almost works
function Set-Shortcut {
param(
[Parameter(ValueFromPipelineByPropertyName=$true)]
$LinkPath,
$Hotkey,
$IconLocation,
$Arguments,
$TargetPath
)
begin {
$shell = New-Object -ComObject WScript.Shell
}
process {
$link = $shell.CreateShortcut($LinkPath)
$PSCmdlet.MyInvocation.BoundParameters.GetEnumerator() |
Where-Object { $_.key -ne 'LinkPath' } |
ForEach-Object { $link.$($_.key) = $_.value }
$link.Save()
}
}
However
Set-Shortcut -LinkPath "C:\Users\user\Desktop\test.lnk" -TargetPath powershell start process "www.youtube.com"
Will default out to attaching a default path if you do not define one to look like:
"C:\Users\micha\Desktop\powershell start process "www.youtube.com""
how do I get rid of that default file path?
BONUS: I'd be appreciative if someone broke down this line of code:
ForEach-Object { $link.$($_.key) = $_.value }
TargetPathshould be just the path to powershell.exe and whatever you want PowerShel tyo do goes in theArguments.ForEach-Object { $link.$($_.key) = $_.value }sets, for each parameter passed (reflected in automatic variable$_) a property with the same name as the parameter to the parameter value. E.g,-TargetPath powerShellresults in$link.TargetPath = 'powershell'.