【发布时间】:2023-03-03 15:11:01
【问题描述】:
如何使用 Windows 7 将某些程序固定到任务栏 电源外壳?请逐步解释。
以及如何修改以下代码以将文件夹固定到 任务栏?例如
$folder = $shell.Namespace('D:\Work')
在此路径中,example 命名文件夹。
【问题讨论】:
标签: powershell windows-7 taskbar
如何使用 Windows 7 将某些程序固定到任务栏 电源外壳?请逐步解释。
以及如何修改以下代码以将文件夹固定到 任务栏?例如
$folder = $shell.Namespace('D:\Work')
在此路径中,example 命名文件夹。
【问题讨论】:
标签: powershell windows-7 taskbar
您可以使用 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()}
【讨论】:
&,因为它用于您可以在文件的右键单击上下文菜单中按下的热键。
另一种方式
$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 下。
【讨论】:
由于我需要通过 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
}
【讨论】: