0

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 }
2
  • 1
    A shortcut to open a website is a different file format alltogether with extension .Url. Why do you want to start PowerShell.exe and have that open the site in the default browser? It can be done, but then the TargetPath should be just the path to powershell.exe and whatever you want PowerShel tyo do goes in the Arguments. Commented May 8, 2022 at 15:39
  • Your sample call is inconsistent with the symptom - it would create an error. 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 powerShell results in $link.TargetPath = 'powershell'. Commented May 8, 2022 at 18:51

1 Answer 1

1

have you tried to change the properties of the shortcut object directly?

Try this and let me know:

function Set-Shortcut {

[CmdletBinding()]
param (
    [Parameter(Mandatory, Position = 0, ValueFromPipeline)]
    [System.String]$FilePath,

    [Parameter(Mandatory, Position = 1)]
    [System.String]$TargetPath
)

Begin {
    $shell = new-object -ComObject WScript.Shell
}

Process {
    try {
        $file = Get-ChildItem -Path $FilePath -ErrorAction Stop

        $shortcut = $shell.CreateShortcut($file.FullName)
        $shortcut.TargetPath = $TargetPath
        $shortcut.Save()    
    }
    catch {
        throw $PSItem
    }
}

End {
    while ($result -ne -1) {
        $result = [System.Runtime.InteropServices.Marshal]::ReleaseComObject($shell)
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

you forgot ONE single curly bracket but it worked perfectly so you are incredibly appreciated
Glad to hear it worked!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.