Search code examples
powershellshortcuttaskbar

Prevent shortcuts pinned to the taskbar by a script from being removed when re-running the script


I have a script that pins application shortcuts to the Windows Taskbar.

The script I have works fine when pinning the shortcuts. However, if the script is run for a second time it will then remove the shortcuts it previously pinned.

The issue appears to be here:

$appWord = "C:\Temp\Word.lnk"
$appWord = "C:\Temp\Excel.lnk"
$apps = @($appWord, $appExcel)

foreach($_ in $apps)
{
($shortcuts.ParseName($_).verbs() | ? {$_.Name -match "Tas&kbar"}).Doit()
}

How can I stop the pinned items being removed if they already exist?


Solution

  • Exclude verbs that have the word "unpin" in them:

    $apps | % {
      $verb = $shortcuts.ParseName($_).verbs() | ? {
        $_.Name -match "Tas&kbar" -and $_.Name -notmatch 'unpin'
      }
      $verb.Doit()
    }