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

标签 powershell windows-10

我正在尝试使用以下代码将程序固定到 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

如您所见,没有将其固定到任务栏的动词。但是,如果我右键单击该特定文件,则会出现以下选项:
Available verbs in UI

问题:
我错过了什么吗?
Windows 10 中是否有将程序固定到任务栏的新方法?

最佳答案

非常好!我对那个 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)
}

关于powershell - 在 Windows 10 中使用 PS 将程序固定到任务栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31720595/

相关文章:

javascript - Cordova 和 Windows 10 Javascript 应用程序有什么区别?

PowerShell Get-ChildItem 并跳过

powershell - PowerShell 中的 bool 文字

Python Popen 在复合命令 (PowerShell) 中失败

c# - 如何在旧版本的 Windows 10 上测试应用程序?

c# - 访问路径被拒绝但文件没有限制

powershell - 停止循环的Powershell脚本并杀死由它启动的远程进程

powershell - 自动 Web 部署到远程 IIS 服务器

windows-10 - 获取可用的屏幕尺寸

c++ - 我无法让 SHGetFileInfo 返回图标位置