21

How can I pin some programs to taskbar on Windows 7 using PowerShell? Please explain step-by-step.

And How to modify the following code to pin a folder to taskbar? For Example

$folder = $shell.Namespace('D:\Work') 

In this path, example named folder.

Günter Zöchbauer
  • 490,478
  • 163
  • 1,733
  • 1,404
cethint
  • 1,871
  • 7
  • 23
  • 30

3 Answers3

24

You can invoke a Verb (Pin to Taskbar) using the Shell.Application COM object. Here's some example code:

http://gallery.technet.microsoft.com/scriptcenter/b66434f1-4b3f-4a94-8dc3-e406eb30b750

The example is somewhat complicated. Here is a simplified version:

$shell = new-object -com "Shell.Application"  
$folder = $shell.Namespace('C:\Windows')    
$item = $folder.Parsename('notepad.exe')
$verb = $item.Verbs() | ? {$_.Name -eq 'Pin to Tas&kbar'}
if ($verb) {$verb.DoIt()}
Steven Penny
  • 82,115
  • 47
  • 308
  • 348
Andy Arismendi
  • 45,027
  • 16
  • 97
  • 114
  • 1
    typo on 'Pin to Tas&kbar' or is 'Pin to Taskbar'? – CB. Mar 16 '12 at 15:25
  • It actually has the `&` because it's used for the hot key you can press in the file's right-click context menu. – Andy Arismendi Mar 16 '12 at 15:26
  • ok, but functions in the link show a $verb.replace( "&","") ... Can't test it now.. – CB. Mar 16 '12 at 15:28
  • Yea, I think just to make it less ugly to pass as a parameter. Strange thing... this is crashing PowerShell in Win8 Beta but works in Win7. It pinned once in Win8 for me but crashed after. – Andy Arismendi Mar 16 '12 at 15:33
  • Ladies and Gentlemen :)It works successfully. Thanks Andy, I think this is the shortest solution for that. – cethint Mar 16 '12 at 15:43
  • 5
    The downside to this approach is that it parses English text to find the verb. The solution from Steven Penny, which uses invokeverb('taskbarpin'), is more appropriate since it uses an identifier that is likely to be the same across locales. – Joe the Coder May 20 '15 at 13:01
  • Could you have a look at my problem?http://stackoverflow.com/questions/37476927/powershell-cannot-find-some-verbs-of-executable/37477179?noredirect=1#comment62452193_37477179 – Lei Yang May 27 '16 at 07:37
22

Another way

$sa = new-object -c shell.application
$pn = $sa.namespace($env:windir).parsename('notepad.exe')
$pn.invokeverb('taskbarpin')

Or unpin

$pn.invokeverb('taskbarunpin')

Note: notepad.exe may not be under %windir%, it may exist under %windir%\system32 for server OS's.

Steven Penny
  • 82,115
  • 47
  • 308
  • 348
10

As I needed to do this via PowerShell, I utilized the methods provided by others here. This is my implementation I ended up adding to a PowerShell module:


function Get-ComFolderItem() {
    [CMDLetBinding()]
    param(
        [Parameter(Mandatory=$true)] $Path
    )

    $ShellApp = New-Object -ComObject 'Shell.Application'

    $Item = Get-Item $Path -ErrorAction Stop

    if ($Item -is [System.IO.FileInfo]) {
        $ComFolderItem = $ShellApp.Namespace($Item.Directory.FullName).ParseName($Item.Name)
    } elseif ($Item -is [System.IO.DirectoryInfo]) {
        $ComFolderItem = $ShellApp.Namespace($Item.Parent.FullName).ParseName($Item.Name)
    } else {
        throw "Path is not a file nor a directory"
    }

    return $ComFolderItem
}

function Install-TaskBarPinnedItem() {
    [CMDLetBinding()]
    param(
        [Parameter(Mandatory=$true)] [System.IO.FileInfo] $Item
    )

    $Pinned = Get-ComFolderItem -Path $Item

    $Pinned.invokeverb('taskbarpin')
}

function Uninstall-TaskBarPinnedItem() {
    [CMDLetBinding()]
    param(
        [Parameter(Mandatory=$true)] [System.IO.FileInfo] $Item
    )

    $Pinned = Get-ComFolderItem -Path $Item

    $Pinned.invokeverb('taskbarunpin')
}

Example usage for a provisioning script:


# The order results in a left to right ordering
$PinnedItems = @(
    'C:\Program Files\Oracle\VirtualBox\VirtualBox.exe'
    'C:\Program Files (x86)\Mozilla Firefox\firefox.exe'
)

# Removing each item and adding it again results in an idempotent ordering
# of the items. If order doesn't matter, there is no need to uninstall the
# item first.
foreach($Item in $PinnedItems) {
    Uninstall-TaskBarPinnedItem -Item $Item
    Install-TaskBarPinnedItem   -Item $Item
}
vmrob
  • 2,706
  • 23
  • 38
  • 1
    Seems that doesn't work ok Win Server 2016 DataCenter. – stej Feb 22 '17 at 06:14
  • Does not do anything on Windows 10 Pro for me. https://stackoverflow.com/questions/31720595/pin-program-to-taskbar-using-ps-in-windows-10 might be an alternative (not tested yet) – Günter Zöchbauer Jun 19 '19 at 15:24