1
+ <#
2
+ . SYNOPSIS
3
+ Pins or unpins a target item to the Windows Taskbar
4
+
5
+ . PARAMETER Target
6
+ Path of the target item to pin to the Taskbar
7
+
8
+ . PARAMETER Unpin
9
+ If true then unpin target from the Taskbar
10
+
11
+ . NOTES
12
+ https://community.spiceworks.com/topic/2165665-pinning-taskbar-items-with-powershell-script
13
+ #>
14
+
15
+ param (
16
+ [parameter (Mandatory = $True , HelpMessage = ' Path of target item to pin to the Taskbar' )]
17
+ [ValidateNotNullOrEmpty ()]
18
+ [string ] $Target ,
19
+
20
+ [Parameter (HelpMessage = ' If true then unpin target from the Taskbar' )]
21
+ [switch ] $Unpin
22
+ )
23
+
24
+ Begin
25
+ {
26
+ function RegisterShellHandlerVerb
27
+ {
28
+ Write-Verbose ' ... registering shell handler verb'
29
+ $guid = (Get-ItemProperty (' HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\' + `
30
+ ' Explorer\CommandStore\shell\Windows.taskbarpin' )
31
+ ).ExplorerCommandHandler
32
+
33
+ $script :shellKey = (Get-Item ' HKCU:\SOFTWARE\Classes'
34
+ ).OpenSubKey(' *' , $true
35
+ ).CreateSubKey(' shell' , $true )
36
+
37
+ $shellKey.CreateSubKey (' {:}' , $true ).SetValue(' ExplorerCommandHandler' , $guid )
38
+ Write-Verbose ' ... shell handler verb registered'
39
+ }
40
+
41
+ function UnregsisterShellHandlerVerb
42
+ {
43
+ if ($shellKey )
44
+ {
45
+ Write-Verbose ' ... unregistering shell handler verb'
46
+ $shellKey.DeleteSubKeyTree (' {:}' , $false )
47
+ $shellKey.Close ()
48
+ $shellKey.Dispose ()
49
+ Write-Verbose ' ... shell handler verb unregistered'
50
+ }
51
+ }
52
+
53
+ function GetTaskbandPinMap
54
+ {
55
+ Write-Verbose ' ... getting Taskband pin map'
56
+ # Taskband\FavoritesResolve is a binary value containing pinned items
57
+ $key = ' HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\Taskband'
58
+ $name = ' FavoritesResolve'
59
+
60
+ # gets contents in ASCII format and filter out non-readable chars so -like will work
61
+ return [Text.Encoding ]::ASCII.GetString(
62
+ (Get-ItemProperty - Path $key - Name $name | Select-Object - ExpandProperty $name )
63
+ ) -Replace ' [^\x20-\x2f^\x30-\x39\x41-\x5A\x61-\x7F]+' , ' '
64
+ }
65
+
66
+ function InvokeShellHandlerVerb
67
+ {
68
+ Write-Verbose ' ... invoking handler verb'
69
+ $item = (Get-Item $Target )
70
+
71
+ (New-Object - ComObject ' Shell.Application'
72
+ ).Namespace($item.DirectoryName
73
+ ).ParseName($item.Name
74
+ ).InvokeVerb(' {:}' )
75
+
76
+ Write-Verbose ' ... handler verb invoked'
77
+ }
78
+ }
79
+ Process
80
+ {
81
+ if (! (Test-Elevated ))
82
+ {
83
+ Write-Warning ' must run from an elevated process'
84
+ }
85
+
86
+ if (Test-Path $Target )
87
+ {
88
+ $Target = (Resolve-Path $Target ).Path
89
+ }
90
+ else
91
+ {
92
+ Write-Warning " $Target does not exist"
93
+ return
94
+ }
95
+
96
+ RegisterShellHandlerVerb
97
+
98
+ $map = GetTaskbandPinMap
99
+
100
+ if ($Unpin )
101
+ {
102
+ # only unpin if item is pinned
103
+ if ($map.Contains ((Split-Path $Target - Leaf)))
104
+ {
105
+ Write-Verbose ' ... unpinning'
106
+ InvokeShellHandlerVerb
107
+ }
108
+ }
109
+ else
110
+ {
111
+ # only pin if item hasn't been pinned
112
+ if (! $map.Contains ((Split-Path $Target - Leaf)))
113
+ {
114
+ Write-Verbose ' ... pinning'
115
+ InvokeShellHandlerVerb
116
+ }
117
+ }
118
+ }
119
+ End
120
+ {
121
+ UnregsisterShellHandlerVerb
122
+ }
0 commit comments