locked
PowerShell-How do I get Link Target for a shortcut? RRS feed

  • Question

  • Hi Forum, 

    I'm new with Powershell on Windows 10 and as exercise I like to produce a small script which
    should get "Link Target" for all shourtcut's in a folder in order for example to produce a playlist * .m3u. 

    I can get all the filenames with [Get-Child Item-name] but how do I get "Link Target"?  

    Thank you in advance for the Tips. 

    Best Regards/Lars

    Wednesday, August 30, 2017 11:49 AM

Answers

  • Hi LarsKong,

    You could try the following script, hope it is helpful to you:
    $WScript = New-Object -ComObject WScript.Shell
    Get-ChildItem -Path "D:\Folder\*.lnk" | ForEach-Object {$WScript.CreateShortcut($_.FullName).TargetPath} | Out-File -FilePath "D:\Folder\playlist.m3u"

    If you need further help, please feel free to let us know.

    Best Regards,
    Albert Ling

    Please remember to mark the replies as an answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Thursday, August 31, 2017 2:47 AM

All replies

    • Proposed as answer by subsum44 Wednesday, August 30, 2017 5:38 PM
    Wednesday, August 30, 2017 12:16 PM
  • Hi LarsKong,

    You could try the following script, hope it is helpful to you:
    $WScript = New-Object -ComObject WScript.Shell
    Get-ChildItem -Path "D:\Folder\*.lnk" | ForEach-Object {$WScript.CreateShortcut($_.FullName).TargetPath} | Out-File -FilePath "D:\Folder\playlist.m3u"

    If you need further help, please feel free to let us know.

    Best Regards,
    Albert Ling

    Please remember to mark the replies as an answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Thursday, August 31, 2017 2:47 AM
  • Hi,

    Just checking in to see if the information provided was helpful. Please let us know if you would like further assistance.

    Best Regards,
    Albert Ling

    Please remember to mark the replies as an answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Tuesday, September 5, 2017 3:06 AM
  • Hi,

    I am checking how the issue is going, if you still have any questions, please feel free to contact us.

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.
    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.
    If no, please reply and tell us the current situation in order to provide further help.

    Appreciate for your feedback.

    Best Regards,
    Albert Ling

    Please remember to mark the replies as an answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Friday, September 8, 2017 7:33 AM
  • Hi Albert, Thanks! I am trying to understand your proposal. Kr / Lars

    Saturday, September 9, 2017 4:55 PM
  • The

    [$WScript = New-Object -ComObject WScript.Shell]

    creates a new instance, I suppose. Why do we need that, please?



    • Edited by LarsKong Saturday, September 9, 2017 5:19 PM
    Saturday, September 9, 2017 5:19 PM
  • The

    [$WScript = New-Object -ComObject WScript.Shell]

    creates a new instance, I suppose. Why do we need that, please?



    That creates the com object that is used to read the "lnk" file.  "lnk" files are not text.  They are special binary files.

    See: https://en.wikipedia.org/wiki/Binary_file

    See: https://support.microsoft.com/en-us/help/242297/how-to-use-windows-script-host-to-create-shortcut-with-parameters


    \_(ツ)_/

    Saturday, September 9, 2017 5:42 PM
  • The scrip is working for me. Thanks a million!
    Saturday, September 9, 2017 6:50 PM
  • So you are "piping" the Get-ChildItem on to the ForEach-Object, which reads the TargetPath and againg you "pipe" that to the Out-File and thereby the playlist *.m3u

    Many Thanks! I would have taken me ages to figure out. I will study the com object a bit more.


    • Edited by LarsKong Saturday, September 9, 2017 6:54 PM
    Saturday, September 9, 2017 6:54 PM
  • I would like to get rid of the drive letter e.g. "C:" in the playlist, is that possible maybe by using select-string or something, please?
    Saturday, September 9, 2017 6:56 PM
  • ok, sorry. I certainly won't do that again :-(
    Sunday, September 10, 2017 6:24 PM