powershell 在Windows 10中使用PS将程序固定到任务栏

hgtggwj0  于 2022-11-10  发布在  Shell
关注(0)|答案(9)|浏览(289)

我正在尝试使用以下代码将一个程序固定到Windows 10(RTM)的任务栏上:

$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');

这在Windows 8.1上有效,但在Windows 10上不再有效。
如果我执行$item.Verbs(),我会得到以下结果:

Application Parent Name
----------- ------ ----
                   &Open
                   Run as &administrator
                   &Pin to Start

                   Restore previous &versions

                   Cu&t
                   &Copy
                   Create &shortcut
                   &Delete
                   Rena&me
                   P&roperties

正如您所看到的,没有动词可以将其固定在任务栏上。但是,如果我右键单击该特定文件,选项就在那里:

问题:

我是不是遗漏了什么?
在Windows 10中,有没有一种将程序固定在任务栏上的新方法?

dfuffjeb

dfuffjeb1#

非常好!我对PowerShell示例做了一些小调整,希望您不介意:)

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)
}
anauzrmj

anauzrmj2#

我有同样的问题,我仍然不知道如何处理它,但这个小命令行工具做到了:
http://www.technosys.net/products/utils/pintotaskbar
您可以像这样在命令行中使用它:

syspin "path/file.exe" c:5386

将程序固定到任务栏并

syspin "path/file.exe" c:5387

来解开它。这对我来说很好。

dvtswwa3

dvtswwa33#

以下是Humberto的VBScript解决方案移植到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)
}
mxg2im7a

mxg2im7a4#

在Windows10中,微软在显示动词之前添加了一个简单的复选标记。可执行文件的名称必须是EXPLORER.EXE。它可以在任何文件夹中,只是名称被选中。因此,在C#或任何编译程序中,最简单的方法就是重命名您的程序。
如果这是不可能的,您可以欺骗外壳对象,使其认为您的程序名为EXPLORER.EXE。我写了一篇文章here,讲述了如何在C#中通过更改PEB中的图像路径来完成此操作。

q7solyqu

q7solyqu5#

抱歉,让这么古老的东西复活了。
我不知道如何在PowerShell中做到这一点,但在VBSCRIPT中,您可以使用我开发的这个方法。无论使用哪种系统语言,它都能正常工作。
可在Windows 8.x和10上运行。

脚本

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
'----------------------------------------------------------------------

命令行:

pin and unpin: taskbarpin.vbs [fullpath]

Example: taskbarpin.vbs "C:\Windows\notepad.exe"
mdfafbf1

mdfafbf16#

参考我调整的@Humberto Freitas答案,你可以尝试这个VBScript,以便在Windows10中使用VBScript将程序固定到任务栏。

  • 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
'----------------------------------------------------------------------
  • 编辑:2020年12月24日*

请参阅:Windows 10中的Microsoft Edge位于何处?我怎么才能启动它?
Microsoft Edge应该在任务栏中。它是蓝色的‘e’图标。

如果您没有或已取消固定,则只需重新固定即可。不幸的是,MicrosoftEdge.exe不能通过双击来运行,并且创建正常的快捷方式将不起作用。你可能就是在这个地方找到的。

您需要做的只是在开始菜单或搜索栏中搜索Edge。看到Microsoft Edge后,右击它,然后固定到任务栏

您可以使用以下VB脚本运行Microsoft Edge:Run-Micro-Edge.vbs

CreateObject("wscript.shell").Run "%windir%\explorer.exe shell:Appsfolder\Microsoft.MicrosoftEdge_8wekyb3d8bbwe!MicrosoftEdge"
6tr1vspr

6tr1vspr7#

我用上面的答案作为动力,写了一个PowerShell类。我只是把它放到一个模块中,然后导入到我的其他脚本中。

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")

下面的模块

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
    }

}
72qzrwbm

72qzrwbm8#

警告:本页上的PowerShell/VBScript答案都不适用于Windows 10 21H2或Windows 11(此功能早在2019年就已中断)。

我只是粗略地说一下,这样人们就不会在这里浪费时间了。希望有人能更新上面的答案(因为它们是很好的答案,但由于微软做出了改变,所以不再起作用了,所以希望对这里的答案进行一些调整,可以让它们重新工作)。由于目前还没有用PowerShell将应用程序固定到任务栏上的已知方法(因为这个页面上的解决方案都不起作用),我知道唯一有效的解决方案是syspin.exe应用程序。请更新PowerShell的答案,因为这比使用syspin要好得多。目前(令人遗憾的是),这个页面主要是一个“陷阱”;人们来到这里,很高兴地找到了这个问题的答案,结果却浪费了他们的时间去尝试那些都坏了的解决方案。

r1zk6ea1

r1zk6ea19#

我建议使用Windows 10的这一功能。它允许人们通过一个XML文件指定固定的程序(和其他东西)。

相关问题