Created
March 16, 2016 01:23
-
-
Save mattmcnabb/a09e5975200e01eedac5 to your computer and use it in GitHub Desktop.
Pin or unpin applications to/from the taskbar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Add-TaskbarItem | |
{ | |
<# | |
.SYNOPSIS | |
Adds or removes items from a user's taskbar | |
.DESCRIPTION | |
Adds or removes items from a user's taskbar. Supports pinning of multiple items using the pipeline. | |
.PARAMETER Action | |
Specifies whether to pin or remove an item from the taskbar. Valid values are "PinToTaskBar" and "UnpinFromTaskBar." | |
.PARAMETER Path | |
Specifies the path to the application to be pinned. | |
.EXAMPLE | |
Add-TaskbarItem -Action PinToTaskBar -Path c:\windows\system32\notepad.exe | |
Pins notepad to the taskbar | |
.EXAMPLE | |
Add-TaskbarItem -Action UnpinFromTaskBar -Path c:\windows\system32\notepad.exe | |
Removes notepad from the taskbar | |
.EXAMPLE | |
'c:\windows\system32\notepad.exe','c:\Windows\System32\ServerManager.exe' | Add-TaskbarItem -Action PinToTaskBar | |
Adds Notepad and Server Manager to the taskbar simultaneously | |
.EXAMPLE | |
'c:\windows\system32\notepad.exe','c:\Windows\System32\ServerManager.exe' | Add-TaskbarItem -Action UnpinFromTaskBar | |
Removes Notepad and Server Manager from the taskbar simultaneously | |
.NOTES | |
Adapted from a script module by Jan Egil-Ring | |
Needs better error handling: | |
- If the item is not currently pinned and you try to unpin it, the error returned is "Verb not found" | |
- Same happens if you try to pin the same item twice | |
.LINK | |
http://blog.crayon.no/blogs/janegil/archive/2010/02/26/pin-and-unpin-applications-from-the-taskbar-and-start-menu-using-windows-powershell.aspx | |
#> | |
[CmdletBinding()] | |
param | |
( | |
[Parameter(Mandatory = $true)] | |
[ValidateSet('PinToTaskbar','UnpinFromTaskbar')] | |
[string] | |
$Action, | |
[Parameter(Mandatory = $true, ValueFromPipeline = $true)] | |
[string] | |
$Path | |
) | |
BEGIN | |
{ | |
function Invoke-Verb | |
{ | |
param | |
( | |
[string] | |
$Path, | |
$Verb | |
) | |
$Verb = $Verb.Replace('&','') | |
$FilePath = Split-Path -Path $Path | |
$Shell = New-Object -ComObject 'Shell.Application' | |
$Folder = $Shell.Namespace($FilePath) | |
$Item = $Folder.Parsename((Split-Path -Path $Path -Leaf)) | |
$ItemVerb = $Item.Verbs() | Where-Object -FilterScript { | |
$_.Name.Replace('&','') -eq $Verb | |
} | |
if ($ItemVerb -eq $null) { throw "Verb $Verb not found." } | |
else { $ItemVerb.DoIt() } | |
} | |
function Get-Verb | |
{ | |
param | |
( | |
[int] | |
$VerbId | |
) | |
try { $t = [type]'CosmosKey.Util.MuiHelper' } | |
catch | |
{ | |
$def = [Text.StringBuilder]'' | |
[void]$def.AppendLine('[DllImport("user32.dll")]') | |
[void]$def.AppendLine('public static extern int LoadString(IntPtr h,uint id, System.Text.StringBuilder sb,int maxBuffer);') | |
[void]$def.AppendLine('[DllImport("kernel32.dll")]') | |
[void]$def.AppendLine('public static extern IntPtr LoadLibrary(string s);') | |
Add-Type -MemberDefinition $def.ToString() -Name MuiHelper -Namespace CosmosKey.Util | |
} | |
if($global:CosmosKey_Utils_MuiHelper_Shell32 -eq $null) | |
{ | |
$global:CosmosKey_Utils_MuiHelper_Shell32 = [CosmosKey.Util.MuiHelper]::LoadLibrary('shell32.dll') | |
} | |
$maxVerbLength = 255 | |
$verbBuilder = New-Object -TypeName Text.StringBuilder -ArgumentList '', $maxVerbLength | |
[void][CosmosKey.Util.MuiHelper]::LoadString($CosmosKey_Utils_MuiHelper_Shell32,$VerbId,$verbBuilder,$maxVerbLength) | |
$verbBuilder.ToString() | |
} | |
} | |
PROCESS | |
{ | |
if (-not (Test-Path $Path)) { throw "Path $Path does not exist." } | |
$verbs = @{ | |
'PinToTaskbar' = 5386 | |
'UnpinfromTaskbar' = 5387 | |
} | |
if($verbs.$Action -eq $null) | |
{ | |
Throw "Action $Action not supported`nSupported actions are:`n`tPintoStartMenu`n`tUnpinfromStartMenu`n`tPintoTaskbar`n`tUnpinfromTaskbar" | |
} | |
Invoke-Verb -Path $Path -Verb $(Get-Verb -VerbId $verbs.$Action) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment