Как подключиться к панели задач с помощью PowerShell
Как я могу подключить некоторые программы к панели задач в Windows 7, используя
PowerShell? Пожалуйста, объясните шаг за шагом.
И как изменить следующий код, чтобы скопировать папку в
панель задач? Для примера
$folder = $shell.Namespace('D:\Work')
В этом пути example
названная папка.
Ответы
Ответ 1
Вы можете вызвать Verb
(Pin to Taskbar) с помощью COM-объекта Shell.Application. Вот пример кода:
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()}
Ответ 2
Другой способ
$sa = new-object -c shell.application
$pn = $sa.namespace($env:windir).parsename('notepad.exe')
$pn.invokeverb('taskbarpin')
Или unin
$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')
}
Пример использования для обеспечения script:
# 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
}