使用 PowerShell 根据快捷方式目标路径取消固定任务栏快捷方式 [英] Use PowerShell to unpin Taskbar shortcut depending on shortcut Target Path

查看:95
本文介绍了使用 PowerShell 根据快捷方式目标路径取消固定任务栏快捷方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

某些 PowerShell 出现问题.基本上,我试图根据快捷方式路径/exe 文件删除某些任务栏快捷方式.

Having an issue with some PowerShell. Basically I am trying to remove certain Taskbar shortcuts based on the shortcuts path/exe file.

我从 AppData 文件夹中获取固定快捷方式及其名称的以下代码:

The following code I have gets the pinned shortcuts and their name from the AppData folder:

Function Get-TaskbarShortcuts
{
Begin{
  Clear-Host
        $Path = "C:\Users\username\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar"
        $x=0
      } # End of Begin

Process
    {
        $TaskbarShortcuts = Get-ChildItem $Path -Recurse -Include *.lnk
            ForEach($ShortCut in $TaskbarShortcuts)
            {
                $Shell = New-Object -ComObject WScript.Shell 
                    $Properties = @{
                    ShortcutName = $Shortcut.Name 
                    LinkTarget = $Shell.CreateShortcut($Shortcut).targetpath 
                                    }
                New-Object PSObject -Property $Properties 
                $x ++
                Write-Host $ShortCut
            } #End of ForEach
        [Runtime.InteropServices.Marshal]::ReleaseComObject($Shell) | Out-Null
    } # End of Process
  End{}
}

我需要修改此代码以删除某些快捷方式...例如,用户已固定C:\Windows\notepad.exe",但将快捷方式命名为奇怪的名称.我将如何从任务栏中删除实际的 pin(不仅仅是删除快捷方式文件)?

I need to amend this code to remove certain shortcuts...say for example, a user has pinned 'C:\Windows\notepad.exe' but called the shortcut Name something odd. How would I remove the actual pin from the taskbar (not just delete the shortcut file)?

推荐答案

从C:\Users\username\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar"中删除快捷方式留下图标在任务栏上但处于不可用状态并单击它会弹出一个错误,提示它不存在并询问您是否要删除它.我猜这是你想要避免的.

Deleting the shortcut from the "C:\Users\username\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar" leaves the icon on the taskbar but in an unusable state and clicking it pops up an error that it doesn't exist and asks if you want to remove it. I'm guessing that this is what you want to avoid.

似乎没有一种简单的方法可以做到这一点,但 TechNet 脚本中心中有一个模块可以解决这个问题:

There doesn't seem to be a simple way to do this, but there's a Module available in the TechNet Script Center that does the trick:

从任务栏和开始菜单固定和取消固定应用程序

###########################################################################" 
 #  
 # 
 # NAME: PinnedApplications.psm1 
 #  
 # AUTHOR: Jan Egil Ring, Crayon 
 # 
 # DATE  : 06.08.2010  
 #  
 # COMMENT: Module with the ability to pin and unpin programs from the taskbar and the Start-menu in Windows 7 and Windows Server 2008 R2. 
 # 
 # This module are based on the Add-PinnedApplication script created by Ragnar Harper and Kristian Svantorp: 
 # http://blogs.technet.com/kristian/archive/2009/04/24/nytt-script-pin-to-taskbar.aspx 
 # http://blog.crayon.no/blogs/ragnar/archive/2009/04/17/pin-applications-to-windows-7-taskbar.aspx 
 # 
 # Johan Akerstrom`s blog: http://cosmoskey.blogspot.com 
 # 
 # For more information, see the following blog post: 
 # http://blog.crayon.no/blogs/janegil/archive/2010/02/26/pin-and-unpin-applications-from-the-taskbar-and-start-menu-using-windows-powershell.aspx 
 # 
 # VERSION HISTORY: 
 # 1.0 17.04.2009 - Initial release by Ragnar Harper and Kristian Svantorp 
 # 1.1 26.02.2010 - Update by Jan Egil Ring. Added the capability to unpin applications. 
 # 1.2 06.08.2010 - Update by Johan Akerstrom. Added full MUI support. 
 #  
 ###########################################################################" 


function Set-PinnedApplication 
{ 
<#  
.SYNOPSIS  
This function are used to pin and unpin programs from the taskbar and Start-menu in Windows 7 and Windows Server 2008 R2 
.DESCRIPTION  
The function have to parameteres which are mandatory: 
Action: PinToTaskbar, PinToStartMenu, UnPinFromTaskbar, UnPinFromStartMenu 
FilePath: The path to the program to perform the action on 
.EXAMPLE 
Set-PinnedApplication -Action PinToTaskbar -FilePath "C:\WINDOWS\system32\notepad.exe" 
.EXAMPLE 
Set-PinnedApplication -Action UnPinFromTaskbar -FilePath "C:\WINDOWS\system32\notepad.exe" 
.EXAMPLE 
Set-PinnedApplication -Action PinToStartMenu -FilePath "C:\WINDOWS\system32\notepad.exe" 
.EXAMPLE 
Set-PinnedApplication -Action UnPinFromStartMenu -FilePath "C:\WINDOWS\system32\notepad.exe" 
#>  
       [CmdletBinding()] 
       param( 
      [Parameter(Mandatory=$true)][string]$Action,  
      [Parameter(Mandatory=$true)][string]$FilePath 
       ) 
       if(-not (test-path $FilePath)) {  
           throw "FilePath does not exist."   
    } 

       function InvokeVerb { 
           param([string]$FilePath,$verb) 
        $verb = $verb.Replace("&","") 
        $path= split-path $FilePath 
        $shell=new-object -com "Shell.Application"  
        $folder=$shell.Namespace($path)    
        $item = $folder.Parsename((split-path $FilePath -leaf)) 
        $itemVerb = $item.Verbs() | ? {$_.Name.Replace("&","") -eq $verb} 
        if($itemVerb -eq $null){ 
            throw "Verb $verb not found."             
        } else { 
            $itemVerb.DoIt() 
        } 

       } 
    function GetVerb { 
        param([int]$verbId) 
        try { 
            $t = [type]"CosmosKey.Util.MuiHelper" 
        } catch { 
            $def = [Text.StringBuilder]"" 
            [void]$def.AppendLine('[DllImport("user32.dll")]') 
            [void]$def.AppendLine('public static extern int LoadString(IntPtr h,uint id, System.Text.StringBuilder sb,int maxBuffer);') 
            [void]$def.AppendLine('[DllImport("kernel32.dll")]') 
            [void]$def.AppendLine('public static extern IntPtr LoadLibrary(string s);') 
            add-type -MemberDefinition $def.ToString() -name MuiHelper -namespace CosmosKey.Util             
        } 
        if($global:CosmosKey_Utils_MuiHelper_Shell32 -eq $null){         
            $global:CosmosKey_Utils_MuiHelper_Shell32 = [CosmosKey.Util.MuiHelper]::LoadLibrary("shell32.dll") 
        } 
        $maxVerbLength=255 
        $verbBuilder = new-object Text.StringBuilder "",$maxVerbLength 
        [void][CosmosKey.Util.MuiHelper]::LoadString($CosmosKey_Utils_MuiHelper_Shell32,$verbId,$verbBuilder,$maxVerbLength) 
        return $verbBuilder.ToString() 
    } 

    $verbs = @{  
        "PintoStartMenu"=5381 
        "UnpinfromStartMenu"=5382 
        "PintoTaskbar"=5386 
        "UnpinfromTaskbar"=5387 
    } 

    if($verbs.$Action -eq $null){ 
           Throw "Action $action not supported`nSupported actions are:`n`tPintoStartMenu`n`tUnpinfromStartMenu`n`tPintoTaskbar`n`tUnpinfromTaskbar" 
    } 
    InvokeVerb -FilePath $FilePath -Verb $(GetVerb -VerbId $verbs.$action) 
} 

Export-ModuleMember Set-PinnedApplication

这篇关于使用 PowerShell 根据快捷方式目标路径取消固定任务栏快捷方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆