forked from bcdady/MyScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGet-Shortcuts.ps1
177 lines (156 loc) · 6.93 KB
/
Get-Shortcuts.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
$ErrorActionPreference = 'Inquire'
Write-Output -InputObject 'Dot-sourcing shortcut functions ...'
Write-Output -InputObject 'Declaring function Get-Shortcut'
function Get-Shortcut {
<#
.SYNOPSIS
Enumerate shortcut files and their attributes, from specified folder(s)
.DESCRIPTION
Expects path to a directory/folder, and returns shortcut file objects for al *.lnk files
.PARAMETER $path
$path specifies the directory or folder to be enumerated
.EXAMPLE
PS .\>Get-Shortcut $env:UserProfile\Desktop
Link : Shortcut.lnk
TargetPath : $env:UserProfile\Downloads\sample_document.pdf
WindowStyle : 1
IconLocation : ,0
Hotkey :
Target : sample_document.pdf
Arguments :
LinkPath : $env:UserProfile\Desktop\Shortcut.lnk
.EXAMPLE
PS .\>Get-Shortcut $env:UserProfile\Desktop | select-object -Property Link, Target, Arguments;
Link Target Arguments
---- ------ ---------
Gartner Toolkit - how to document.lnk toolkit_how_to_document_your_239747.zip
Git Shell.lnk GitHub.appref-ms --open-shell
GoToMeeting.lnk g2mstart.exe "/Action Host" "/Trigger Shortcut" "/
.NOTES
NAME : Get-Shortcut
VERSION : 1.0.0.0
LAST UPDATED: 11/1/2015
AUTHOR : @bcdady
#>
param(
[Parameter(
Position = 0,
ValueFromPipeline = $true,
HelpMessage = 'Folder path to look for .lnk shortcuts'
)]
[ValidateScript({Test-Path -Path $PSItem})]
[string]
$path = "$Env:ProgramData\Microsoft\Windows\Start Menu\Programs" # $(Join-Path -Path $env:UserProfile -ChildPath 'Desktop')
)
$WSCShell = New-Object -ComObject WScript.Shell
<# if ($path -eq $null) {
$pathUser = [System.Environment]::GetFolderPath('Desktop')
# $pathCommon = $WSCShell.SpecialFolders.Item('AllUsersStartMenu')
$path = Get-ChildItem $pathUser, $pathCommon -Filter *.lnk -Recurse
}
#>
$Private:RetObject = New-Object -TypeName PSObject
$global:shortcuts = @()
Write-Debug -Message "Get-ChildItem $path -Filter *.lnk"
Get-ChildItem $path -Filter *.lnk -Recurse | ForEach-Object -Process {
Write-Debug -Message "Getting shortcut info for $($PSItem.FullName)"
$link = $WSCShell.CreateShortcut($PSItem.FullName)
$linkArguments = ''
try
{
Test-Path -Path $link.TargetPath -PathType Leaf | out-null
$linkTarget = Split-Path $link.TargetPath -Leaf
if ($link.Arguments) { $linkArguments = $link.Arguments }
$private:properties = [ordered]@{
'Name' = $($PSItem.Name -replace "\s",'').Replace('.lnk','')
'Target' = $linkTarget
'Arguments' = $linkArguments
'Description' = $link.Description
'FullName' = $link.FullName
}
# 'BaseName' = $($link.FullName | Split-Path $link.TargetPath -Parent)
# Instantiate custom object with these properties
$Private:RetObject = New-Object -TypeName PSObject -Property $private:properties
# Append the current object instance to the collection of objects to be returned
$global:shortcuts += $Private:RetObject
}
catch
{
Write-Debug -Message "Skipping invalid shortcut -- Failed to validate TargetPath for $($PSItem.FullName)"
}
}
# Get-Shortcut $env:UserProfile\Desktop; # | select-object -Property Link, Target, Arguments;
# | Where-Object ($_.Target -eq 'pnagent.exe'); # | Format-Table -Property Link, Target, Arguments -AutoSize
# $Path = "$Env:ProgramData\Microsoft\Windows\Start Menu\Programs"
# Got all local shortcuts ... originally created to 'harvest' shortcuts from a XenApp / RDS server session.
<#$global:shortcuts = @{} # new hash table
Get-ChildItem -Path $Path -Filter *.lnk -Recurse |
foreach {
# Write-Output -InputObject " > $(Split-Path -Path $(Split-Path -Path $PSItem.FullName -Parent) -Leaf) : "
Get-Shortcut -Path $PSItem.FullName | Sort-Object -Unique -Descending |
ForEach-Object -Process {
try {
$global:shortcuts += @{$PSItem.Target = $PSItem.LinkPath} # $shortcuts +
Write-Debug -Message "Added shortcuts target: $($PSItem.Target) with LinkPath $($PSItem.LinkPath)"
}
catch {
Write-Debug -Message "Warning: Duplicate shortcut target or other unexpected issue with PSItem:`n$PSItem"
}
}
}
#>
Write-Output -InputObject "Successfully mapped $($global:shortcuts.Count) shortcuts."
}
Write-Output -InputObject 'Declaring function Start-Shortcut'
function Start-Shortcut {
param(
[Parameter(
Position = 0,
ValueFromPipeline = $true,
HelpMessage = 'Folder path to look for .lnk shortcuts'
)]
[ValidateScript({foreach ($key in $global:shortcuts.Name) { Write-Output ('Comparing {0}* with {1}' -f $key, $PSItem); if ($myApp -like "$PSItem*") { Write-Output -InputObject "$myApp matched $app"; return $true } }})]
[string]
$Name = 'SnippingTool'
)
Write-Output -InputObject "Starting $Name : $($shortcuts.$Name)"
& $shortcuts.$Name
}
# Get-Shortcut $env:UserProfile\Desktop; # | select-object -Property Link, Target, Arguments;
# | Where-Object ($_.Target -eq 'pnagent.exe'); # | Format-Table -Property Link, Target, Arguments -AutoSize
Write-Output -InputObject 'Declaring function Show-Shortcut'
function Show-Shortcut {
param(
[Parameter(
Position = 0,
ValueFromPipeline = $true,
HelpMessage = 'Folder path to look for .lnk shortcuts'
)]
[string]
$Key = 'SnippingTool',
[Parameter(
Mandatory = $false,
Position = 1
)]
[switch]
$All = $false
)
# [ValidateScript({$global:shortcuts.Name -in "$PSItem*"})]
if ($All) {
foreach ($sc in $($global:shortcuts.Name | Sort-Object -Property Values)) {
$showPath = $global:shortcuts.$sc -replace 'C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\'
$props = @{
ShortcutName = $sc.Name
LinkTarget = $showPath
}
New-Object -TypeName psobject -Property $props
}
} else {
$showPath = $global:shortcuts.$Key -replace 'C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\'
$props = @{
ShortcutName = $Key
LinkTarget = $showPath
}
New-Object -TypeName psobject -Property $props
}
}