How to pin to taskbar using PowerShell

白昼怎懂夜的黑 提交于 2019-11-30 12:40:09

问题


How can I pin some programs to taskbar on Windows 7 using PowerShell? Please explain step-by-step.

And How to modify the following code to pin a folder to taskbar? For Example

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

In this path, example named folder.


回答1:


You can invoke a Verb (Pin to Taskbar) using the Shell.Application COM object. Here's some example code:

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

The example is somewhat complicated. Here is a simplified version:

$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:


Another way

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

Or unpin

$pn.invokeverb('taskbarunpin')

Note: notepad.exe may not be under %windir%, it may exist under %windir%\system32 for server OS's.




回答3:


As I needed to do this via PowerShell, I utilized the methods provided by others here. This is my implementation I ended up adding to a PowerShell module:


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')
}

Example usage for a provisioning 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
}


来源:https://stackoverflow.com/questions/9739772/how-to-pin-to-taskbar-using-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!