pcoul pcoul - 1 year ago 105
PowerShell Question

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?

Answer Source

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

$apps | % {
  $verb = $shortcuts.ParseName($_).verbs() | ? {
    $_.Name -match "Tas&kbar" -and $_.Name -notmatch 'unpin'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download