Author Posts

January 1, 2012 at 12:00 am

by rob.irvin at 2013-02-13 16:44:22

Hi.

I'm trying to find the target of all the shortcuts that show up on the start menu. So far I have this:

$sc = Get-ChildItem -recurse "C:\ProgramData\Microsoft\Windows\Start Menu\Programs" -include *.lnk
$sh = New-Object -COM WScript.Shell

foreach ($_ in $sc)
{$sh.CreateShortcut($_).targetpath}

This will list all the targets but what I'm wanting is to have it also list the name of the shortcut along with the target and write it to a text file.

Something like:
"my shortcut.lnk"
"c:\program files\my_application\my_application.exe"

"myshortcut2.lnk"
"c:\program files\my_application2\my_application2.exe"

I don't really care what format the file is in, I just want to know which target goes with each shortcut file. Any help is appreciated.

Thanks,
Rob

by MattG at 2013-02-13 18:38:52

Hi Rob,

Personally, I would craft a custom object consisting of the shortcut name and its target path. I would then write the output to a CSV file using the following code:
function Get-StartMenuShortcuts
{
$Shortcuts = Get-ChildItem -Recurse "$Env:ProgramData\Microsoft\Windows\Start Menu\Programs" -Include *.lnk
$Shell = New-Object -ComObject WScript.Shell

foreach ($Shortcut in $Shortcuts)
{
$Properties = @{
ShortcutName = $Shortcut.Name
Target = $Shell.CreateShortcut($Shortcut).targetpath
}

New-Object PSObject -Property $Properties
}

[Runtime.InteropServices.Marshal]::ReleaseComObject($Shell) | Out-Null
}

$Output = Get-StartMenuShortcuts
$Output

$Output | Export-Csv -LiteralPath .\shortcuts_targets.csv

by rob.irvin at 2013-02-14 08:39:18

Thanks! That is exactly what I need. The only thing, when I ran it in Powershell plus IDE, it complained about the last line "-literalPath"
Anyway I slightly changed the last line and it works perfectly. Thank you so much.

$Output | Export-Csv -path C:\temp\shortcuts.csv -NoTypeInformation

-Rob