【问题标题】:How to pin to taskbar using PowerShell如何使用 PowerShell 固定到任务栏
【发布时间】:2023-03-03 15:11:01
【问题描述】:

如何使用 Windows 7 将某些程序固定到任务栏 电源外壳?请逐步解释。

以及如何修改以下代码以将文件夹固定到 任务栏?例如

$folder = $shell.Namespace('D:\Work') 

在此路径中,example 命名文件夹。

【问题讨论】:

    标签: powershell windows-7 taskbar


    【解决方案1】:

    您可以使用 Shell.Application COM 对象调用Verb(固定到任务栏)。下面是一些示例代码:

    http://gallery.technet.microsoft.com/scriptcenter/b66434f1-4b3f-4a94-8dc3-e406eb30b750

    这个例子有点复杂。这是一个简化的版本:

    $shell = new-object -com "Shell.Application"  
    $folder = $shell.Namespace('C:\Windows')    
    $item = $folder.Parsename('notepad.exe')
    $verb = $item.Verbs() | ? {$_.Name -eq 'Pin to Tas&kbar'}
    if ($verb) {$verb.DoIt()}
    

    【讨论】:

    • 'Pin to Tas&kbar' 上的错字还是'Pin to Taskbar'?
    • 它实际上有&,因为它用于您可以在文件的右键单击上下文菜单中按下的热键。
    • 好的,但是链接中的函数显示 $verb.replace("&","") ...现在无法测试..
    • 是的,我认为只是为了减少作为参数传递的丑陋。奇怪的事情......这是在 Win8 Beta 中崩溃的 PowerShell,但在 Win7 中有效。它为我在 Win8 中固定过一次,但之后就崩溃了。
    • 这种方法的缺点是它会解析英文文本来查找动词。 Steven Penny 使用 invokeverb('taskbarpin') 的解决方案更合适,因为它使用的标识符在不同地区可能相同。
    【解决方案2】:

    另一种方式

    $sa = new-object -c shell.application
    $pn = $sa.namespace($env:windir).parsename('notepad.exe')
    $pn.invokeverb('taskbarpin')
    

    或取消固定

    $pn.invokeverb('taskbarunpin')
    

    注意:notepad.exe 可能不在%windir% 下,它可能存在于服务器操作系统的%windir%\system32 下。

    【讨论】:

      【解决方案3】:

      由于我需要通过 PowerShell 执行此操作,因此我使用了此处其他人提供的方法。这是我最终添加到 PowerShell 模块的实现:

      
      function Get-ComFolderItem() {
          [CMDLetBinding()]
          param(
              [Parameter(Mandatory=$true)] $Path
          )
      
          $ShellApp = New-Object -ComObject 'Shell.Application'
      
          $Item = Get-Item $Path -ErrorAction Stop
      
          if ($Item -is [System.IO.FileInfo]) {
              $ComFolderItem = $ShellApp.Namespace($Item.Directory.FullName).ParseName($Item.Name)
          } elseif ($Item -is [System.IO.DirectoryInfo]) {
              $ComFolderItem = $ShellApp.Namespace($Item.Parent.FullName).ParseName($Item.Name)
          } else {
              throw "Path is not a file nor a directory"
          }
      
          return $ComFolderItem
      }
      
      function Install-TaskBarPinnedItem() {
          [CMDLetBinding()]
          param(
              [Parameter(Mandatory=$true)] [System.IO.FileInfo] $Item
          )
      
          $Pinned = Get-ComFolderItem -Path $Item
      
          $Pinned.invokeverb('taskbarpin')
      }
      
      function Uninstall-TaskBarPinnedItem() {
          [CMDLetBinding()]
          param(
              [Parameter(Mandatory=$true)] [System.IO.FileInfo] $Item
          )
      
          $Pinned = Get-ComFolderItem -Path $Item
      
          $Pinned.invokeverb('taskbarunpin')
      }
      

      配置脚本的示例用法:

      
      # The order results in a left to right ordering
      $PinnedItems = @(
          'C:\Program Files\Oracle\VirtualBox\VirtualBox.exe'
          'C:\Program Files (x86)\Mozilla Firefox\firefox.exe'
      )
      
      # Removing each item and adding it again results in an idempotent ordering
      # of the items. If order doesn't matter, there is no need to uninstall the
      # item first.
      foreach($Item in $PinnedItems) {
          Uninstall-TaskBarPinnedItem -Item $Item
          Install-TaskBarPinnedItem   -Item $Item
      }
      

      【讨论】:

      • Win Server 2016 DataCenter 好像不行。
      • 在 Windows 10 Pro 上没有为我做任何事情。 stackoverflow.com/questions/31720595/… 可能是另一种选择(尚未测试)
      • 这些都不适用于德国 Windows 11。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-07
      • 1970-01-01
      • 1970-01-01
      • 2020-06-05
      • 2023-02-20
      • 2011-10-15
      • 2019-12-31
      相关资源
      最近更新 更多