PoshCode Archive  Artifact [5360bfec8c]

Artifact 5360bfec8c0d8b4bfed47c2c7432b4777938257060c87cdad305c84725385814:

  • File Search-StartMenu.ps1 — part of check-in [0b26434a8c] at 2018-06-10 13:07:10 on branch trunk — From Windows PowerShell Cookbook (O’Reilly) by Lee Holmes (user: Lee Holmes size: 2040)

# encoding: ascii
# api: powershell
# title: Search-StartMenu.ps1
# description: From Windows PowerShell Cookbook (O’Reilly) by Lee Holmes
# version: 0.1
# type: script
# author: Lee Holmes
# license: CC0
# x-poshcode-id: 2210
# x-archived: 2016-03-19T02:55:49
# x-published: 2011-09-09T21:42:00
#
#
##############################################################################
##
## Search-StartMenu
##
## From Windows PowerShell Cookbook (O'Reilly)
## by Lee Holmes (http://www.leeholmes.com/blog)
##
##############################################################################

<#

.SYNOPSIS

Search the Start Menu for items that match the provided text. This script
searches both the name (as displayed on the Start Menu itself,) and the
destination of the link.

.Example

Search-StartMenu "Character Map" | Invoke-Item
Searches for the "Character Map" appication, and then runs it

Search-StartMenu PowerShell | Select-FilteredObject | Invoke-Item
Searches for anything with "PowerShell" in the application name, lets you
pick which one to launch, and then launches it.

#>

param(
    ## The pattern to match
    [Parameter(Mandatory = $true)]
    $Pattern
)

Set-StrictMode -Version Latest

## Get the locations of the start menu paths
$myStartMenu = [Environment]::GetFolderPath("StartMenu")
$shell = New-Object -Com WScript.Shell
$allStartMenu = $shell.SpecialFolders.Item("AllUsersStartMenu")

## Escape their search term, so that any regular expression
## characters don't affect the search
$escapedMatch = [Regex]::Escape($pattern)

## Search for text in the link name
dir $myStartMenu *.lnk -rec | ? { $_.Name -match "$escapedMatch" }
dir $allStartMenu *.lnk -rec | ? { $_.Name -match "$escapedMatch" }

## Search for text in the link destination
dir $myStartMenu *.lnk -rec |
    Where-Object { $_ | Select-String "\\[^\\]*$escapedMatch\." -Quiet }
dir $allStartMenu *.lnk -rec |
    Where-Object { $_ | Select-String "\\[^\\]*$escapedMatch\." -Quiet }