Quantcast
Channel: Scripting – Robin CM's IT Blog
Viewing all articles
Browse latest Browse all 27

Powershell function to pin and unpin from Windows Taskbar

$
0
0

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


Viewing all articles
Browse latest Browse all 27

Trending Articles