Skip to content

Instantly share code, notes, and snippets.

@labmonkey
Last active May 2, 2024 11:35
Show Gist options
  • Select an option

  • Save labmonkey/72b7016e2fc5d836dda27e199c017d86 to your computer and use it in GitHub Desktop.

Select an option

Save labmonkey/72b7016e2fc5d836dda27e199c017d86 to your computer and use it in GitHub Desktop.
Script to manage Windows Taskbar Pinned Icons and Shortcuts

This script is a collection of methods that allow you to programatically manage your Windows taskbar icons.

The main purpose of this is to help the people who are using corporate laptops where the IT is forcefully managing the pinned icons. In my case I get the default pinned icons every time I reboot my laptop which is annoying.

There are many ways to run this script but I am using the only one that was not blocked on PC by IT which is to run Win+R and type there shell:startup. In the directory that just opened you can place the script or a Shortcut to it and then it will execute on every startup (might be delayed a bit depending on hoe many apps ru nat startup).

You can also see that I keep the script runing continuously in the background because in my case the IT is updating taskbar every 100 minutes so now I revert to my setup everr minute if needed.

$BasePath = $PSScriptRoot
# Removes the Search icon from Taskbar
Function Remove-Search {
# Define the registry path and property name
$RegistryPath = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Search'
$PropertyName = 'SearchboxTaskbarMode'
$currentValue = (Get-ItemProperty -Path $RegistryPath -Name $PropertyName).$PropertyName
$NewValue = '0'
If ($currentValue -eq 1) {
Write-Output "Updating SearchboxTaskbarMode"
Set-ItemProperty -Path $RegistryPath -Name $PropertyName -Value $NewValue
}
}
# If your PC is managed by an organization (at work) then they might use this file to define default pinned items
Function Remove-Defaults {
$replacementText = '<!-- Removed CustomTaskbarLayoutCollection -->'
$filename = "$env:LOCALAPPDATA\Microsoft\Windows\Shell\LayoutModification.xml"
if (!(Select-String -Path $filename -Pattern $replacementText -Quiet)) {
Write-Output "Updating LayoutModification.xml"
$regex = '(?ms)(\n^\s*<CustomTaskbarLayoutCollection\s*?).*(CustomTaskbarLayoutCollection>)'
# Remove the CustomTaskbarLayoutCollection entry
(Get-Content -raw $filename) -replace $regex, $replacementText | Set-Content $filename
}
}
# Windows Taskbar pinned icons are stored in registry key and directory with shortcuts.
#
# Important: You need to maintain both of these in order for the shortcuts to work.
Function Backup-Taskbar {
$BackupDir = "$BasePath\Backup"
$RegistryFile = "$BackupDir\taskbar-favourites.reg"
$ShortCutDir = "$env:APPDATA\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar"
# Create Backup Directory
if (-not (Test-Path -Path $BackupDir)) {
Write-Output "Creating $BackupDir"
New-Item -ItemType Directory -Force -Path $BackupDir | Out-Null
}
If (!(Test-Path $RegistryFile -PathType Leaf)) {
Write-Output "Creating Taskbar backup"
Invoke-Command { reg export 'HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Taskband' $RegistryFile /y }
If (Test-Path $BackupDir\TaskBar) { Remove-Item -Recurse $BackupDir\TaskBar }
New-Item -ItemType Directory -Force -Path $BackupDir\TaskBar | Out-Null
Copy-item -Force -Recurse $ShortCutDir\*.lnk -Destination $BackupDir\TaskBar
}
# Checking if registry keys are different in the .reg file seems to be more complicated so I didn't bother
Invoke-Command { reg import $RegistryFile }
Copy-item -Force -Recurse $BackupDir\TaskBar\* -Destination $ShortCutDir
}
# Refresh the Taskbar if icons have changed
#
# This function need to be executed only if Backup-Taskbar is used
Function Restart-Explorer {
get-process explorer | Stop-Process -Force
}
# These can be just done once on init (they don't change in my case)
Backup-Taskbar
Restart-Explorer
# Run it in background periodically (if cannot use Windows Services)
# At my workplace they revert these every 100 minutes...
while ($true) {
Remove-Search
Remove-Defaults
Start-Sleep -Seconds 60
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment