PowerShell Function – List Shortcuts

Create a PowerShell Function to List Menu ShortcutsPowerShell Function List Shortcuts

The purpose of this code is to enumerate the start menu items, and also show the path to the program behind each link.

PowerShell Function Get-StartMenu

List Shortcuts Under the Start Menu

Here is a break-down of the tasks for our project to list all the shortcuts found on the Start Menu.  The trickiest part is locating the underlying program for each .lnk file.

  1. Create a function: Any name will do, but let us honor PowerShell’s verb-noun cmdlet convention and call it Get-StartMenu
  2. Use the $Env: variable to locate the StartMenu\Programs
  3. Remember to drill down from the Start Menu.  Let us also filter the .lnk files.
  4. Get the name of executable behind the shortcut link. [Tricky]
  5. Sort the shortcuts, and then tabulate the path to the underlying program.

# Use the $Env: variable to locate the StartMenu\Programs
$Path = "$Env:ProgramData\Microsoft\Windows\Start Menu\Programs"
Get-ChildItem $Path -Recurse -Include *.lnk

Snippet which uses -ComObject to locate the TargetPath

$Shell = New-Object -ComObject WScript.Shell
$Properties = @{
ShortcutName = $Shortcut.Name
LinkTarget = $Shell.CreateShortcut($Shortcut).targetpath
}

Putting it All Together – List Shortcuts and Targets

The function we are creating will be called Get-StartMenu; observe how the last two lines call the function, sort the names, and tabulate the path to the targets.

# PowerShell function to list Start Menu Shortcuts
Function Get-StartMenu{
Begin{
Clear-Host
$Path = "$Env:ProgramData\Microsoft\Windows\Start Menu\Programs"
$x=0
      } # End of Begin
Process {
$StartMenu = Get-ChildItem $Path -Recurse -Include *.lnk
ForEach($ShortCut in $StartMenu) {
$Shell = New-Object -ComObject WScript.Shell
$Properties = @{
ShortcutName = $Shortcut.Name
LinkTarget = $Shell.CreateShortcut($Shortcut).targetpath
}
New-Object PSObject -Property $Properties
$x ++
        } #End of ForEach
[Runtime.InteropServices.Marshal]::ReleaseComObject($Shell) | Out-Null
    } # End of Process
End{
"`nStart Menu items = $x "
     }
}
#Example of function in action:

Get-StartMenu | Sort ShortcutName | Ft ShortcutName, LinkTarget -Auto

Note 1: Observe how the ForEach loop combines the shortcut’s name to its target path.

Note 2: This line is just to count the shortcuts
"`nStart Menu items = $x "  (`n) means new line.

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds’ Network Performance Monitor

Get-StartMenu Function Structure

At its simplest the function just has a name and a list of statements:

Get-StartMenu {
Statements 1
Statement 2 …
}

The reason that I created this function was to combine two ideas, getting the shortcut name with Get-ChildItem, and discovering the underlying executable with New-Object -ComObject.

Using a function is easy, just type it's name!

See more on PowerShell functions »

Summary of PowerShell Function to List Start Menu Shortcuts

This script draws together several PowerShell techniques, creating a function, using the $Env: variable, and drawing from New-Object -ComObject; the result is a fine scripts that lists the path to the Start Menu shortcuts.

If you like this page then please share it with your friends

 


See More Windows PowerShell Examples Functions

Scripting PowerShell Function   • PowerShell Create Shortcut   • PowerShell Function Shortcut

PowerShell Tutorials  • PowerShell Temp  • PowerShell Get-Item Env:  • Clear-WinEvent Function

Create PowerShell Function  • PowerShell Examples  • PowerShell Function Format DiskSize

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.