PowerShell - Get FULL TargetPath of Shortcut

Page 1 of 3 123 LastLast

  1. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
       #1

    PowerShell - Get FULL TargetPath of Shortcut


    Good evening,

    As I have setup a LOT of Shortcuts in a specific folder, I wanted to find a way of extracting specific data from EACH of them, which I have managed to 90% achieve.

    The only part that I am having difficulty with is the data that is in the TargetPath field, because it excludes ANY data [ commands ] after a specific point.

    I have setup a .BAT file and a .PS1 file for this which obviously works from having the SAME Filename.

    BAT
    Code:
    
    @echo off & mode con: cols=30 lines=1 & color A & C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe -ExecutionPolicy Bypass %~dp0%~n0.ps1

    PS1
    Code:
    
    Function Get-DesktopShortcuts{
      $Shortcuts = Get-ChildItem -Recurse "C:\System-Admin_ToolBar" -Include *.lnk
      $Shell = New-Object -ComObject WScript.Shell
      ForEach ($Shortcut in $Shortcuts)
      {
        $Properties = @{
        ShortcutName = $Shortcut.Name;
        ShortcutFull = $Shortcut.FullName;
        ShortcutPath = $shortcut.DirectoryName;
        Target       = $Shell.CreateShortcut($Shortcut).TargetPath
        }
      New-Object PSObject -Property $Properties
      }
      [Runtime.InteropServices.Marshal]::ReleaseComObject($Shell) | Out-Null
    }
    $Output = Get-DesktopShortcuts
    $Output | Out-GridView ;Pause

    It Outputs the data in GridView.

    As an EXAMPLE of my problem, if I was to use my Shortcut for Disk Cleanup - Advanced Mode, the GridView for Target shows . . .

    PowerShell - Get FULL TargetPath of Shortcut-ps_grid-view_1.jpg

    But, in the Properties of the Shortcut, there is extra data AFTER the \cleanmgr.exe . . .

    PowerShell - Get FULL TargetPath of Shortcut-ps_grid-view_2.jpg

    So my question is, how do I get it to show the FULL data in the TargetPath field [ the /sagerun:65535 ] please.

    EVERYTHING works except that particular bit.

    Thanks.
      My Computer


  2. Posts : 14,019
    Win10 Pro and Home, Win11 Pro and Home, Win7, Linux Mint
       #2

    This is an old post but maybe something there:
    https://answers.microsoft.com/en-us/...5-6d3d5e5a626c
    I don't have a clue as to what the number on yours is supposed to do so maybe I'll learn.
      My Computers


  3. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
    Thread Starter
       #3

    Thanks for the reply @Berton,

    Berton said:
    This is an old post but maybe something there: . . . I don't have a clue as to what the number on yours does.

    My problem is NOT with ANY of the Shortcuts, they ALL work perfectly.

    What I want, is for the Target field in the output in GridView to show the EXTRA commands at the end of the Target field, the SAME as they are shown in the Properties of the Shortcut.
      My Computer


  4. Posts : 3,274
    Win10
       #4

    Maybe something along these lines to somehow get the 'arguments'
    Code:
    ForEach ($Shortcut in $Shortcuts)
      {
        $Properties = @{
        ShortcutName = $Shortcut.Name;
        ShortcutFull = $Shortcut.FullName;
        ShortcutPath = $shortcut.DirectoryName;
        Target       = $Shell.CreateShortcut($Shortcut).TargetPath
        Arguments    = $Shell.CreateShortcut($Shortcut).Arguments
            }
    Get target of shortcut (.lnk) file with powershell - Stack Overflow

    ps.
    Sorry, Paul, that then doesn't show the paths !
      My Computers


  5. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
    Thread Starter
       #5

    Hello @das10,

    das10 said:
    Maybe something along these lines to somehow get the 'arguments' . . .

    Code:
        Arguments    = $Shell.CreateShortcut($Shortcut).Arguments

    P.S. Sorry, Paul, that then doesn't show the paths !

    Brilliant, that is EXACTLY what I was after. Is there any way to get the Arguments column AFTER the Target column please?

    If you think that you can get the Target and Arguments into one column, say at the end of the Output, that would just be a BONUS.

    Thanks.

      My Computer


  6. Posts : 3,274
    Win10
       #6

    Perhaps:
    Code:
    {
        $Properties = @{
        ShortcutName = $Shortcut.Name;
        ShortcutFull = $Shortcut.FullName;
        ShortcutPath = $shortcut.DirectoryName;
        Target       = $Shell.CreateShortcut($Shortcut).TargetPath + " " + $Shell.CreateShortcut($Shortcut).Arguments
     }
    Last edited by das10; 14 Sep 2021 at 16:38. Reason: correction to relevant code per next post
      My Computers


  7. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
    Thread Starter
       #7

    Hello @das10,

    das10 said:
    Perhaps:
    Code:
    {
        $Properties = @{
        ShortcutName = $Shortcut.Name;
        ShortcutFull = $Shortcut.FullName;
        ShortcutPath = $shortcut.DirectoryName;
        Target       = $Shell.CreateShortcut($Shortcut).TargetPath + " " + $Shell.CreateShortcut($Shortcut).Arguments
            }
      New-Object PSObject -Property $Properties
      }
      New-Object PSObject -Property $Properties
      }
    Yes indeed.

    Funny thing was that I was working on the first post and when I checked the output, I also noticed that there was a space needed, which I added. When I looked back at your post, you has ALSO added it. Great minds think alike as they say.

    The FINAL thing I need to do is to get the output to AutoSize the Columns.

    Did you mean to change . . .

    Code:
    
        }
      New-Object PSObject -Property $Properties
      }
      [Runtime.InteropServices.Marshal]::ReleaseComObject($Shell) | Out-Null
    }

    . . . to . . .

    Code:
    
            }
      New-Object PSObject -Property $Properties
      }
      New-Object PSObject -Property $Properties
      }
    

    . . . does it still clear the Array?

    I really must look into PowerShell more, although I am a bit old in the tooth now.

    Thanks again, it is appreciated.

      My Computer


  8. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
    Thread Starter
       #8

    Well, I ended up with the following which I am more than happy with . . .

    Code:
    Function Get-DesktopShortcuts{
      $Shortcuts = Get-ChildItem -Recurse $Env:SystemDrive\System-Admin_ToolBar -Include *.lnk
      $Shell = New-Object -ComObject WScript.Shell
      ForEach ($Shortcut in $Shortcuts)
      {
        $Properties = @{
        ShortcutName = $Shortcut.Name;
        ShortcutFull = $Shortcut.FullName;
        ShortcutPath = $shortcut.DirectoryName;
        Target       = $Shell.CreateShortcut($Shortcut).TargetPath + " " + 
                       $Shell.CreateShortcut($Shortcut).Arguments
        }
        New-Object PSObject -Property $Properties
      }
      [Runtime.InteropServices.Marshal]::ReleaseComObject($Shell) | Out-Null
    }
    $Output = Get-DesktopShortcuts
    $Output | Out-GridView
    $Output | Export-CSV $Env:UserProfile\Desktop\'Sys Admin TB Shortcuts.ods' ;Pause

    I will however, investigate on getting the GridView output to AutoSize tomorrow.

    Thank for ALL your help @das10, it is very much appreciated.

    I am marking this thread as Solved.

    Last edited by Paul Black; 15 Sep 2021 at 02:32.
      My Computer


  9. Posts : 14,019
    Win10 Pro and Home, Win11 Pro and Home, Win7, Linux Mint
       #9

    Paul Black said:
    although I am a bit old in the tooth now.
    And how many decades in a bit? I'm in my 8th now.
      My Computers


  10. Posts : 3,274
    Win10
       #10

    Paul:
    The replacement should have been for this block ONLY (as in your original post) :
    Code:
     {
    $Properties = @{
    ShortcutName = $Shortcut.Name;
    ShortcutFull = $Shortcut.FullName;
    ShortcutPath = $shortcut.DirectoryName;
    Target       = $Shell.CreateShortcut($Shortcut).TargetPath + " " + $Shell.CreateShortcut($Shortcut).Arguments
    }
    I seem to have added an extra line when edit pasting over the old in Post #6 (was in a bit of a hurry), although it still seems to work !
      My Computers


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 22:11.
Find Us




Windows 10 Forums