Created
February 5, 2019 16:48
-
-
Save sheldonhull/0fbe0840ecbdd1d1d95563fd3e63d449 to your computer and use it in GitHub Desktop.
Pin and unpin apps with powershell to start menu and/or taskbar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.Description | |
Pin and unpin apps to start menu and/or taskbar. | |
.Example | |
PS> Pin-App "Microsoft Edge" -unpin -start | |
Unpin Edge | |
.Example | |
PS> Pin-App -AppName "Microsoft Edge" -Start -Taskbar | |
Pin Edge to start menu and the taskbar | |
.NOTES | |
Originally pulled from https://www.tenforums.com/customization/21002-how-automatically-cmd-powershell-script-unpin-all-apps-start.html | |
#> | |
function Pin-App ([string]$appname, [switch]$unpin, [switch]$start, [switch]$taskbar, [string]$path) { | |
if ($unpin.IsPresent) { | |
$action = "Unpin" | |
} else { | |
$action = "Pin" | |
} | |
if (-not $taskbar.IsPresent -and -not $start.IsPresent) { | |
Write-Error "Specify -taskbar and/or -start!" | |
} | |
if ($taskbar.IsPresent) { | |
try { | |
$exec = $false | |
if ($action -eq "Unpin") { | |
((New-Object -Com Shell.Application).NameSpace('shell:::{4234d49b-0245-4df3-b780-3893943456e1}').Items() | ?{$_.Name -eq $appname}).Verbs() | ?{$_.Name.replace('&','') -match 'Unpin from taskbar'} | %{$_.DoIt(); $exec = $true} | |
if ($exec) { | |
Write "App '$appname' unpinned from Taskbar" | |
} else { | |
if (-not $path -eq "") { | |
Pin-App-by-Path $path -Action $action | |
} else { | |
Write "'$appname' not found or 'Unpin from taskbar' not found on item!" | |
} | |
} | |
} else { | |
((New-Object -Com Shell.Application).NameSpace('shell:::{4234d49b-0245-4df3-b780-3893943456e1}').Items() | ?{$_.Name -eq $appname}).Verbs() | ?{$_.Name.replace('&','') -match 'Pin to taskbar'} | %{$_.DoIt(); $exec = $true} | |
if ($exec) { | |
Write "App '$appname' pinned to Taskbar" | |
} else { | |
if (-not $path -eq "") { | |
Pin-App-by-Path $path -Action $action | |
} else { | |
Write "'$appname' not found or 'Pin to taskbar' not found on item!" | |
} | |
} | |
} | |
} catch { | |
Write-Error "Error Pinning/Unpinning $appname to/from taskbar!" | |
} | |
} | |
if ($start.IsPresent) { | |
try { | |
$exec = $false | |
if ($action -eq "Unpin") { | |
((New-Object -Com Shell.Application).NameSpace('shell:::{4234d49b-0245-4df3-b780-3893943456e1}').Items() | ?{$_.Name -eq $appname}).Verbs() | ?{$_.Name.replace('&','') -match 'Unpin from Start'} | %{$_.DoIt(); $exec = $true} | |
if ($exec) { | |
Write "App '$appname' unpinned from Start" | |
} else { | |
if (-not $path -eq "") { | |
Pin-App-by-Path $path -Action $action -start | |
} else { | |
Write "'$appname' not found or 'Unpin from Start' not found on item!" | |
} | |
} | |
} else { | |
((New-Object -Com Shell.Application).NameSpace('shell:::{4234d49b-0245-4df3-b780-3893943456e1}').Items() | ?{$_.Name -eq $appname}).Verbs() | ?{$_.Name.replace('&','') -match 'Pin to Start'} | %{$_.DoIt(); $exec = $true} | |
if ($exec) { | |
Write "App '$appname' pinned to Start" | |
} else { | |
if (-not $path -eq "") { | |
Pin-App-by-Path $path -Action $action -start | |
} else { | |
Write "'$appname' not found or 'Pin to Start' not found on item!" | |
} | |
} | |
} | |
} catch { | |
Write-Error "Error Pinning/Unpinning $appname to/from Start!" | |
} | |
} | |
} | |
function Pin-App-by-Path([string]$Path, [string]$Action, [switch]$start) { | |
if ($Path -eq "") { | |
Write-Error -Message "You need to specify a Path" -ErrorAction Stop | |
} | |
if ($Action -eq "") { | |
Write-Error -Message "You need to specify an action: Pin or Unpin" -ErrorAction Stop | |
} | |
if ((Get-Item -Path $Path -ErrorAction SilentlyContinue) -eq $null){ | |
Write-Error -Message "$Path not found" -ErrorAction Stop | |
} | |
$Shell = New-Object -ComObject "Shell.Application" | |
$ItemParent = Split-Path -Path $Path -Parent | |
$ItemLeaf = Split-Path -Path $Path -Leaf | |
$Folder = $Shell.NameSpace($ItemParent) | |
$ItemObject = $Folder.ParseName($ItemLeaf) | |
$Verbs = $ItemObject.Verbs() | |
if ($start.IsPresent) { | |
switch($Action){ | |
"Pin" {$Verb = $Verbs | Where-Object -Property Name -EQ "&Pin to Start"} | |
"Unpin" {$Verb = $Verbs | Where-Object -Property Name -EQ "Un&pin from Start"} | |
default {Write-Error -Message "Invalid action, should be Pin or Unpin" -ErrorAction Stop} | |
} | |
} else { | |
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 Path" -ErrorAction Stop | |
} else { | |
$Result = $Verb.DoIt() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment