2012-03-16 31 views
12

Làm cách nào để ghim một số chương trình vào thanh tác vụ trên Windows 7 bằng cách sử dụng PowerShell? Vui lòng giải thích từng bước một.Cách ghim vào thanh tác vụ bằng PowerShell

Và cách sửa đổi mã sau đây để ghim thư mục thành thanh tác vụ? Ví dụ:

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

Trong đường dẫn này, example thư mục có tên.

Trả lời

22

Bạn có thể gọi Verb (Ghim vào thanh tác vụ) bằng cách sử dụng đối tượng COM Shell.Application. Dưới đây là một số mã mẫu:

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

Ví dụ này hơi phức tạp. Đây là một phiên bản đơn giản:

$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()} 
+0

typo trên 'Pin to Tas & kbar' hoặc là 'Pin to Taskbar'? –

+0

Nó thực sự có '&' vì nó được sử dụng cho phím nóng mà bạn có thể nhấn trong trình đơn ngữ cảnh nhấp chuột phải của tệp. –

+0

ok, nhưng chức năng trong liên kết hiển thị $ verb.replace ("&", "") ... Không thể kiểm tra nó ngay bây giờ .. –

14

Một cách khác

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

Hoặc unpin

$pn.invokeverb('taskbarunpin') 

Lưu ý: notepad.exe có thể không dưới %windir%, nó có thể tồn tại dưới %windir%\system32 cho máy chủ hệ điều hành .

6

Vì tôi cần thực hiện điều này thông qua PowerShell, tôi đã sử dụng các phương pháp được cung cấp bởi những người khác tại đây. Đây là triển khai thực hiện của tôi, tôi đã kết thúc thêm vào một module 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') 
} 

sử dụng Ví dụ cho một kịch bản dự phòng:


# 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 
} 
+0

Có vẻ như không hoạt động OK Trung tâm Dữ liệu Win Server 2016. – stej