Pin program to taskbar using PS in Windows 10
PowershellWindows 10Powershell Problem Overview
I am trying to pin a program to the taskbar in Windows 10 (RTM) using this code:
$shell = new-object -com "Shell.Application"
$folder = $shell.Namespace((Join-Path $env:SystemRoot System32\WindowsPowerShell\v1.0))
$item = $folder.Parsename('powershell_ise.exe')
$item.invokeverb('taskbarpin');
This worked on Windows 8.1, but no longer works on Windows 10.
If I execute $item.Verbs()
, I get these:
Application Parent Name
----------- ------ ----
&Open
Run as &administrator
&Pin to Start
Restore previous &versions
Cu&t
&Copy
Create &shortcut
&Delete
Rena&me
P&roperties
As you can see, there is no verb for pinning it to the taskbar. If I right click that specific file, however, the option is there:
Questions:
Am I missing something?
Is there a new way in Windows 10 to pin a program to the taskbar?
Powershell Solutions
Solution 1 - Powershell
Very nice! I made a few small tweaks to that powershell example, I hope you don't mind :)
param (
[parameter(Mandatory=$True, HelpMessage="Target item to pin")]
[ValidateNotNullOrEmpty()]
[string] $Target
)
if (!(Test-Path $Target)) {
Write-Warning "$Target does not exist"
break
}
$KeyPath1 = "HKCU:\SOFTWARE\Classes"
$KeyPath2 = "*"
$KeyPath3 = "shell"
$KeyPath4 = "{:}"
$ValueName = "ExplorerCommandHandler"
$ValueData =
(Get-ItemProperty `
("HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\" + `
"CommandStore\shell\Windows.taskbarpin")
).ExplorerCommandHandler
$Key2 = (Get-Item $KeyPath1).OpenSubKey($KeyPath2, $true)
$Key3 = $Key2.CreateSubKey($KeyPath3, $true)
$Key4 = $Key3.CreateSubKey($KeyPath4, $true)
$Key4.SetValue($ValueName, $ValueData)
$Shell = New-Object -ComObject "Shell.Application"
$Folder = $Shell.Namespace((Get-Item $Target).DirectoryName)
$Item = $Folder.ParseName((Get-Item $Target).Name)
$Item.InvokeVerb("{:}")
$Key3.DeleteSubKey($KeyPath4)
if ($Key3.SubKeyCount -eq 0 -and $Key3.ValueCount -eq 0) {
$Key2.DeleteSubKey($KeyPath3)
}
Solution 2 - Powershell
Here's Humberto's vbscript solution ported to PowerShell:
Param($Target)
$KeyPath1 = "HKCU:\SOFTWARE\Classes"
$KeyPath2 = "*"
$KeyPath3 = "shell"
$KeyPath4 = "{:}"
$ValueName = "ExplorerCommandHandler"
$ValueData = (Get-ItemProperty("HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\" +
"Explorer\CommandStore\shell\Windows.taskbarpin")).ExplorerCommandHandler
$Key2 = (Get-Item $KeyPath1).OpenSubKey($KeyPath2, $true)
$Key3 = $Key2.CreateSubKey($KeyPath3, $true)
$Key4 = $Key3.CreateSubKey($KeyPath4, $true)
$Key4.SetValue($ValueName, $ValueData)
$Shell = New-Object -ComObject "Shell.Application"
$Folder = $Shell.Namespace((Get-Item $Target).DirectoryName)
$Item = $Folder.ParseName((Get-Item $Target).Name)
$Item.InvokeVerb("{:}")
$Key3.DeleteSubKey($KeyPath4)
if ($Key3.SubKeyCount -eq 0 -and $Key3.ValueCount -eq 0) {
$Key2.DeleteSubKey($KeyPath3)
}
Solution 3 - Powershell
I have the same problem and I still do not know how to handle it, but this little command line tool does:
http://www.technosys.net/products/utils/pintotaskbar
You can use it in command line like that:
syspin "path/file.exe" c:5386
to pin a program to taskbar and
syspin "path/file.exe" c:5387
to unpin it. This works fine for me.
Solution 4 - Powershell
In windows 10 Microsoft added a simple check before showing the verb. The name of the executable must be explorer.exe. It can be in any folder, just the name is checked. So the easy way in C# or any compiled program would be just to rename your program.
If that's not possible, you can fool the shell object in to thinking your program is called explorer.exe. I wrote a post here on how to do it in C# by changing the Image Path in the PEB.
Solution 5 - Powershell
Sorry to resurrect something so old.
I do not know how to do this in powershell, but in vbscript you can do this method that I developed. It works regardless of the system language.
Works on windows 8.x and 10.
Script
If WScript.Arguments.Count < 1 Then WScript.Quit
'----------------------------------------------------------------------
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFile = WScript.Arguments.Item(0)
sKey1 = "HKCU\Software\Classes\*\shell\{:}\\"
sKey2 = Replace(sKey1, "\\", "\ExplorerCommandHandler")
'----------------------------------------------------------------------
With WScript.CreateObject("WScript.Shell")
KeyValue = .RegRead("HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer" & _
"\CommandStore\shell\Windows.taskbarpin\ExplorerCommandHandler")
.RegWrite sKey2, KeyValue, "REG_SZ"
With WScript.CreateObject("Shell.Application")
With .Namespace(objFSO.GetParentFolderName(objFile))
With .ParseName(objFSO.GetFileName(objFile))
.InvokeVerb("{:}")
End With
End With
End With
.Run("Reg.exe delete """ & Replace(sKey1, "\\", "") & """ /F"), 0, True
End With
'----------------------------------------------------------------------
Command line:
pin and unpin: taskbarpin.vbs [fullpath]
Example: taskbarpin.vbs "C:\Windows\notepad.exe"
Solution 6 - Powershell
Refer to @Humberto Freitas answer that i tweaked for my aim, you can try this vbscript in order to pin a program to taskbar using Vbscript in Windows 10.
Vbscript : TaskBarPin.vbs
Option Explicit
REM Question Asked here ==>
REM https://stackoverflow.com/questions/31720595/pin-program-to-taskbar-using-ps-in-windows-10/34182076#34182076
Dim Title,objFSO,ws,objFile,sKey1,sKey2,KeyValue
Title = "Pin a program to taskbar using Vbscript in Windows 10"
'----------------------------------------------------------------------
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set Ws = CreateObject("WScript.Shell")
objFile = DeQuote(InputBox("Type the whole path of the program to be pinned or unpinned !",Title,_
"%ProgramFiles%\windows nt\accessories\wordpad.exe"))
REM Examples
REM "%ProgramFiles%\Mozilla Firefox\firefox.exe"
REM "%ProgramFiles%\Google\Chrome\Application\chrome.exe"
REM "%ProgramFiles%\windows nt\accessories\wordpad.exe"
REM "%Windir%\Notepad.exe"
ObjFile = ws.ExpandEnvironmentStrings(ObjFile)
If ObjFile = "" Then Wscript.Quit()
sKey1 = "HKCU\Software\Classes\*\shell\{:}\\"
sKey2 = Replace(sKey1, "\\", "\ExplorerCommandHandler")
'----------------------------------------------------------------------
With CreateObject("WScript.Shell")
KeyValue = .RegRead("HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer" & _
"\CommandStore\shell\Windows.taskbarpin\ExplorerCommandHandler")
.RegWrite sKey2, KeyValue, "REG_SZ"
With CreateObject("Shell.Application")
With .Namespace(objFSO.GetParentFolderName(objFile))
With .ParseName(objFSO.GetFileName(objFile))
.InvokeVerb("{:}")
End With
End With
End With
.Run("Reg.exe delete """ & Replace(sKey1, "\\", "") & """ /F"), 0, True
End With
'----------------------------------------------------------------------
Function DeQuote(S)
If Left(S,1) = """" And Right(S, 1) = """" Then
DeQuote = Trim(Mid(S, 2, Len(S) - 2))
Else
DeQuote = Trim(S)
End If
End Function
'----------------------------------------------------------------------
EDIT : on 24/12/2020
Refer to : Where is Microsoft Edge located in Windows 10? How do I launch it?
Microsoft Edge should be in the taskbar. It is the blue 'e' icon.
If you do not have that or have unpinned it, you just need to repin it. Unfortunately the MicrosoftEdge.exe
can not be run by double clicking and creating a normal shortcut will not work. You may have found it at this location.
What you need to do is just search for Edge in the Start menu or search bar. Once you see Microsoft Edge, right click on it and Pin to taskbar.
You can run the Microsoft Edge with this vbscript : Run-Micro-Edge.vbs
CreateObject("wscript.shell").Run "%windir%\explorer.exe shell:Appsfolder\Microsoft.MicrosoftEdge_8wekyb3d8bbwe!MicrosoftEdge"
Solution 7 - Powershell
I wrote a Powershell Class using the above answers as motivation. I just put it into a module then imported into my other scripts.
using module "C:\Users\dlambert\Desktop\Devin PC Setup\PinToTaskbar.psm1"
[PinToTaskBar_Verb] $pin = [PinToTaskBar_Verb]::new();
$pin.Pin("C:\Windows\explorer.exe")
$pin.Pin("$env:windir\system32\SnippingTool.exe")
$pin.Pin("C:\Windows\explorer.exe")
$pin.Pin("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe")
$pin.Pin("C:\Program Files\Notepad++\notepad++.exe")
$pin.Pin("$env:windir\system32\WindowsPowerShell\v1.0\PowerShell_ISE.exe")
Module below
class PinToTaskBar_Verb
{
[string]$KeyPath1 = "HKCU:\SOFTWARE\Classes"
[string]$KeyPath2 = "*"
[string]$KeyPath3 = "shell"
[string]$KeyPath4 = "{:}"
[Microsoft.Win32.RegistryKey]$Key2
[Microsoft.Win32.RegistryKey]$Key3
[Microsoft.Win32.RegistryKey]$Key4
PinToTaskBar_Verb()
{
$this.Key2 = (Get-Item $this.KeyPath1).OpenSubKey($this.KeyPath2, $true)
}
[void] InvokePinVerb([string]$target)
{
Write-Host "Pinning $target to taskbar"
$Shell = New-Object -ComObject "Shell.Application"
$Folder = $Shell.Namespace((Get-Item $Target).DirectoryName)
$Item = $Folder.ParseName((Get-Item $Target).Name)
$Item.InvokeVerb("{:}")
}
[bool] CreatePinRegistryKeys()
{
$TASKBARPIN_PATH = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\Windows.taskbarpin";
$ValueName = "ExplorerCommandHandler"
$ValueData = (Get-ItemProperty $TASKBARPIN_PATH).ExplorerCommandHandler
Write-Host "Creating Registry Key: $($this.Key2.Name)\$($this.KeyPath3)"
$this.Key3 = $this.Key2.CreateSubKey($this.KeyPath3, $true)
Write-Host "Creating Registry Key: $($this.Key3.Name)\$($this.KeyPath4)"
$this.Key4 = $this.Key3.CreateSubKey($this.KeyPath4, $true)
Write-Host "Creating Registry Key: $($this.Key4.Name)\$($valueName)"
$this.Key4.SetValue($ValueName, $ValueData)
return $true
}
[bool] DeletePinRegistryKeys()
{
Write-Host "Deleting Registry Key: $($this.Key4.Name)"
$this.Key3.DeleteSubKey($this.KeyPath4)
if ($this.Key3.SubKeyCount -eq 0 -and $this.Key3.ValueCount -eq 0)
{
Write-Host "Deleting Registry Key: $($this.Key3.Name)"
$this.Key2.DeleteSubKey($this.KeyPath3)
}
return $true
}
[bool] Pin([string]$target)
{
try
{
$this.CreatePinRegistryKeys()
$this.InvokePinVerb($target)
}
finally
{
$this.DeletePinRegistryKeys()
}
return $true
}
}
Solution 8 - Powershell
I recommend to use this Windows 10 feature. It allows people to specify pinned programs (and other things) via an XML file.