-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Set-PinTaskbar.ps1
147 lines (125 loc) · 4.21 KB
/
Set-PinTaskbar.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
<#
.SYNOPSIS
Pins or unpins a target item to the Windows Taskbar
.PARAMETER Target
Path of the target item to pin to the Taskbar
.PARAMETER Unpin
If true then unpin target from the Taskbar
.NOTES
https://community.spiceworks.com/topic/2165665-pinning-taskbar-items-with-powershell-script
https://github.com/gunnarhaslinger/Add-or-Remove-Application-To-Windows-10-Taskbar/blob/master/TaskbarPinning.ps1
Handling Add-/Remove-TaskbarPinningApp can only be done by Processes named "explorer.exe"
Workaround to do this with Powershell: Make a copy of PowerShell to $env:TEMP\explorer.exe
#>
param(
[parameter(Mandatory=$True, HelpMessage='Path of target item to pin to the Taskbar')]
[ValidateNotNullOrEmpty()]
[string] $Target,
[Parameter(HelpMessage='If true then unpin target from the Taskbar')]
[switch] $Unpin
)
Begin
{
function RegisterShellHandlerVerb
{
Write-Verbose '... registering shell handler verb'
$guid = (Get-ItemProperty ('HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\' + `
'Explorer\CommandStore\shell\Windows.taskbarpin')
).ExplorerCommandHandler
$script:shellKey = (Get-Item 'HKCU:\SOFTWARE\Classes'
).OpenSubKey('*', $true
).CreateSubKey('shell', $true)
$shellKey.CreateSubKey('{:}', $true).SetValue('ExplorerCommandHandler', $guid)
Write-Verbose '... shell handler verb registered'
}
function UnregsisterShellHandlerVerb
{
if ($shellKey)
{
Write-Verbose '... unregistering shell handler verb'
$shellKey.DeleteSubKeyTree('{:}', $false)
if ($shellKey.SubKeyCount -eq 0 -and $shellKey.ValueCount -eq 0)
{
(Get-Item 'HKCU:\SOFTWARE\Classes'
).OpenSubKey('*', $true
).DeleteSubKey("shell")
}
Write-Verbose '... shell handler verb unregistered'
}
}
function GetTaskbandPinMap
{
Write-Verbose '... getting Taskband pin map'
# Taskband\FavoritesResolve is a binary value containing pinned items
$key = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\Taskband'
$name = 'FavoritesResolve'
# gets contents in ASCII format and filter out non-readable chars so -like will work
return [Text.Encoding]::ASCII.GetString(
(Get-ItemProperty -Path $key -Name $name | Select-Object -ExpandProperty $name)
) -Replace '[^\x20-\x2f^\x30-\x39\x41-\x5A\x61-\x7F]+', ''
}
function InvokeShellHandlerVerb
{
Write-Verbose '... invoking handler verb'
$item = (Get-Item $Target)
(New-Object -ComObject 'Shell.Application'
).Namespace($item.DirectoryName
).ParseName($item.Name
).InvokeVerb('{:}')
Write-Verbose '... handler verb invoked'
}
}
Process
{
if (!(Test-Elevated))
{
Write-Warning 'must run from an elevated process'
}
if ((get-process | ? { $_.id -eq $pid }).ProcessName -ne 'explorer')
{
# presume current process is powershell
$psh = (gcim win32_process | ? { $_.ProcessId -eq $pid }).CommandLine
# restart this script, emulating the process name as 'explorer'
Copy-Item $psh $env:TEMP\explorer.exe -Force
$splat = @{
Target = $Target
Unpin = "`$$Unpin"
Verbose = "`$$($PSCmdlet.MyInvocation.BoundParameters['Verbose'].IsPresent)"
}
. $env:TEMP\explorer.exe Set-PinTaskbar @splat
return
}
if (Test-Path $Target)
{
$Target = (Resolve-Path $Target).Path
}
else
{
Write-Warning "$Target does not exist"
return
}
RegisterShellHandlerVerb
$map = GetTaskbandPinMap
if ($Unpin)
{
# only unpin if item is pinned
if ($map.Contains((Split-Path $Target -Leaf)))
{
Write-Verbose '... unpinning'
InvokeShellHandlerVerb
}
}
else
{
# only pin if item hasn't been pinned
if (!$map.Contains((Split-Path $Target -Leaf)))
{
Write-Verbose '... pinning'
InvokeShellHandlerVerb
}
}
}
End
{
UnregsisterShellHandlerVerb
}