0

我可以使用 PowerShell 将一些程序固定到 Win7 上的任务栏。

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

如何修改上述代码以将程序固定到“开始”菜单?

4

5 回答 5

5

另一种方式

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

或取消固定

$pn.invokeverb('startunpin')
于 2014-02-22T16:13:42.997 回答
3

使用下面的代码

$shell = new-object -com "Shell.Application"  
$folder = $shell.Namespace('C:\Windows')    
$item = $folder.Parsename('notepad.exe')
$verb = $item.Verbs() | ? {$_.Name -eq 'Pin to Start Men&u'}
if ($verb) {$verb.DoIt()}

注意:更改在第四行。

于 2012-10-10T09:48:39.607 回答
1

在此处查看脚本(国际):http: //gallery.technet.microsoft.com/scriptcenter/b66434f1-4b3f-4a94-8dc3-e406eb30b750

如果您想在现代 UI 界面 (Windows 8) 中添加 Pin 之类的操作,请在 $verbs 处添加 51201

于 2013-05-07T12:56:23.490 回答
1

大多数解决方案的主要问题是它们枚举文件上的动词,搜索执行操作的字符串(“Pin to Startmenu”等),然后执行它。如果您需要在公司中支持 30 多种语言,这将不起作用,除非您使用外部函数来搜索本地化命令(请参阅shtako-verflow 的答案)。

Steven Penny的回答是第一个语言中立的,不需要任何外部代码。它使用存储在注册表中的动词HKEY_CLASSES_ROOT\CLSID\{90AA3A4E-1CBA-4233-B8BB-535773D48449}HKEY_CLASSES_ROOT\CLSID\{a2a9545d-a0c2-42b4-9708-a0b2badd77c8}

基于此,这是我们现在使用的代码:

function PinToTaskbar {
         param([Parameter(Mandatory=$true)][string]$FilePath)  
  ExecuteVerb $FilePath "taskbarpin" 
}

function UnpinFromTaskbar {
         param([Parameter(Mandatory=$true)][string]$FilePath)  
  ExecuteVerb $FilePath "taskbarunpin" 
}

function PinToStartmenu {
         param([Parameter(Mandatory=$true)][string]$FilePath)  
 ExecuteVerb $FilePath "startpin" 
}

function UnpinFromStartmenu {
         param([Parameter(Mandatory=$true)][string]$FilePath)  
 ExecuteVerb $FilePath "startunpin" 
}

function ExecuteVerb {  
         param(
            [Parameter(Mandatory=$true)][string]$File,
            [Parameter(Mandatory=$true)][string]$Verb
         ) 

    $path = [System.Environment]::ExpandEnvironmentVariables($File) 
    $basePath = split-path $path -parent   #retrieve only the path File=C:\Windows\notepad.exe -> C:\Windows
    $targetFile = split-path $path -leaf    #retrieve only the file File=C:\Windows\notepad.exe -> notepad.exe

    $shell = new-object -com "Shell.Application"  
    $folder = $shell.Namespace($basePath)        
    if ($folder)
    {
       $item = $folder.Parsename($targetFile) 

       if ($item)
       {
         $item.invokeverb($Verb)
         # "This method does not return a value." (http://msdn.microsoft.com/en-us/library/windows/desktop/bb787816%28v=vs.85%29.aspx)
         # Therefore we have no chance to know if this was successful...
         write-host "Method [$Verb] executed for [$path]"       
       } 
       else
       {
         write-host "Target file [$targetFile] not found, aborting" 
       }             
    }
    else 
    {      
      write-host "Folder [$basePath] not found, aborting" 
    }

}


#PinToTaskbar "%WINDIR%\notepad.exe"
#UnpinFromTaskbar "%WINDIR%\notepad.exe"

PinToStartmenu "%WINDIR%\notepad.exe"
#UnpinFromStartmenu "%WINDIR%\notepad.exe"
于 2014-02-27T09:40:16.610 回答
0

上面 Steven Penny 的第二个答案对我来说效果很好。这里还有一些花絮。

它通过 PowerShell 执行 COM,因此您几乎可以对任何 COM 客户端执行相同的操作。例如,这是一个 AutoHotkey 版本。

Shell := ComObjCreate("Shell.Application")
Target := Shell.Namespace(EnvGet("WinDir")).ParseName("Notepad.exe")
Target.InvokeVerb("startpin")

除了用于创建对象的函数外,VBScript 或 InnoSetup 看起来几乎相同。

我还发现我有一个固定好的程序,但由于编译器的限制,没有正确的图标和/或描述。我刚刚制作了一个小小的 1 行 WinForms 应用程序,它使用 Process.Start 启动目标,然后在 AppInfo.cs 的 Title 属性的开始菜单中添加了适当的图标和我想要的名称。

于 2014-03-04T04:01:24.427 回答