Powershell function to pin and unpin from Windows Taskbar

Wrote this to make it easy to add and remove pinned items from the taskbar.

Code

function Pin-Taskbar([string]$Item = "",[string]$Action = ""){
    if($Item -eq ""){
        Write-Error -Message "You need to specify an item" -ErrorAction Stop
    }
    if($Action -eq ""){
        Write-Error -Message "You need to specify an action: Pin or Unpin" -ErrorAction Stop
    }
    if((Get-Item -Path $Item -ErrorAction SilentlyContinue) -eq $null){
        Write-Error -Message "$Item not found" -ErrorAction Stop
    }
    $Shell = New-Object -ComObject "Shell.Application"
    $ItemParent = Split-Path -Path $Item -Parent
    $ItemLeaf = Split-Path -Path $Item -Leaf
    $Folder = $Shell.NameSpace($ItemParent)
    $ItemObject = $Folder.ParseName($ItemLeaf)
    $Verbs = $ItemObject.Verbs()
    switch($Action){
        "Pin"   {$Verb = $Verbs | Where-Object -Property Name -EQ "Pin to Tas&kbar"}
        "Unpin" {$Verb = $Verbs | Where-Object -Property Name -EQ "Unpin from Tas&kbar"}
        default {Write-Error -Message "Invalid action, should be Pin or Unpin" -ErrorAction Stop}
    }
    if($Verb -eq $null){
        Write-Error -Message "That action is not currently available on this item" -ErrorAction Stop
    } else {
        $Result = $Verb.DoIt()
    }
}

Syntax

Pin-Taskbar -Item  -Action <Pin|Unpin>

Usage examples

Pin MS Paint to the taskbar

Pin-Taskbar -Item "C:\Windows\System32\mspaint.exe" -Action Pin

Unpin MS Paint from the taskbar

Pin-Taskbar -Item "C:\Windows\System32\mspaint.exe" -Action Unpin
This entry was posted in Applications, PowerShell, Scripting, Windows and tagged , , , , , , , . Bookmark the permalink.

2 Responses to Powershell function to pin and unpin from Windows Taskbar

  1. Pingback: Powershell functions to pin and unpin from Windows Taskbar | ItForDummies

  2. Shawn R says:

    Thanks, this was very short, succinct, and did what needed to be done. There is one thing I found out. On older PS versions (PS 2.0), the where-object’s “-Property” does not work. I change it to a Where-Object {$_.Name…} and it worked fine from PS 2.0 through PS 5.0. Thanks again!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s