none
Modify a shortcut "Target" field to accept parameters

    Question

  • Hi,

    I am very new to powershell and am trying to modify a shortcut. In my research I have found that this is not possible but recreating the shortcut is a workaround. however I am having dificulty doing this.

    I need the target field to read something like this:
    "c:\MyPath\Variable" -s

    note the inverted commas. and Variable is an actual variable set prior to running the code. This is my code so far. I am using powershell with -s simply as an example.

    $File = read-host -prompt "Please input filename here..."
    $strDesktopFolder = [System.Environment]::GetFolderPath("Desktop")
    $objShell = New-Object -com "Wscript.Shell"
    $objShortcut = $objShell.CreateShortcut($strDesktopFolder + "\PowerShell.lnk")
    $objShortcut.TargetPath = '"c:\Windows\System32\WindowsPowerShell\v1.0\'+$File+'"'+" -s"
    $objShortcut.Save()

    The output of the Target is as follows:
    "C:\"c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -s"

    The problem is the initial "C:\ and also at the end the final "

    Please could someone assist with this.
    Friday, September 11, 2009 12:46 PM

Answers

  • Hi HiTeeGuy,

    IIRC, the WshShortcut object's CreateShortcut method is poorly named, as it can also open a shortcut (.lnk file) for modification.

    Also, I don't think you want to "hard-code" path names such as "c:\Windows" in a script. It is much better to use the appropriate variables (e.g. $env:SystemRoot or $PSHOME).

    Another thing to remember with the WshShortcut object: Use the Arguments property for command-line parameters. TargetPath is only for the program name. Here's a fixed version of what you're trying to do:

    $file = read-host -prompt "Enter filename"
    $desktopPath = [System.Environment]::GetFolderPath("Desktop")
    $wshShell = new-object -com "Wscript.Shell"
    $lnkFile = $wshShell.CreateShortcut((join-path $desktopPath "Test.lnk"))
    $lnkFile.TargetPath = (join-path $PSHOME $file)
    $lnkFile.Arguments = "-s"
    $lnkFile.Save()

    HTH,

    Bill
    Friday, September 11, 2009 2:50 PM

All replies

  • Hi HiTeeGuy,

    IIRC, the WshShortcut object's CreateShortcut method is poorly named, as it can also open a shortcut (.lnk file) for modification.

    Also, I don't think you want to "hard-code" path names such as "c:\Windows" in a script. It is much better to use the appropriate variables (e.g. $env:SystemRoot or $PSHOME).

    Another thing to remember with the WshShortcut object: Use the Arguments property for command-line parameters. TargetPath is only for the program name. Here's a fixed version of what you're trying to do:

    $file = read-host -prompt "Enter filename"
    $desktopPath = [System.Environment]::GetFolderPath("Desktop")
    $wshShell = new-object -com "Wscript.Shell"
    $lnkFile = $wshShell.CreateShortcut((join-path $desktopPath "Test.lnk"))
    $lnkFile.TargetPath = (join-path $PSHOME $file)
    $lnkFile.Arguments = "-s"
    $lnkFile.Save()

    HTH,

    Bill
    Friday, September 11, 2009 2:50 PM
  • Hi Bill,

    Thanks so much for the help, I have now got it working as required. As I said I am new to powershell and still have a lot to learn. Thanks for the quick response

    Monday, September 14, 2009 6:58 PM
  • Hi HiTeeGuy,

    Great, glad to hear you have it working. This is actually a great example of being able to use an ActiveX (i.e., COM) object in PowerShell. You don't have to have PowerShell to use the WshShortcut object -- a WSH script can do the same thing. But it's great that you're learning PowerShell -- it's worth the investment.

    Bill
    Wednesday, September 16, 2009 7:31 PM